-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwrapper_controls_test.go
More file actions
627 lines (564 loc) · 19 KB
/
wrapper_controls_test.go
File metadata and controls
627 lines (564 loc) · 19 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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
package gopus
import (
"encoding/binary"
"errors"
"sort"
"strings"
"testing"
encodercore "github.com/thesyncim/gopus/encoder"
"github.com/thesyncim/gopus/internal/dnnblob"
"github.com/thesyncim/gopus/internal/dred/rdovae"
"github.com/thesyncim/gopus/internal/lpcnetplc"
)
type optionalEncoderControl interface {
SetDNNBlob([]byte) error
}
type qextEncoderControl interface {
SetQEXT(bool) error
QEXT() (bool, error)
}
type optionalDecoderControl interface {
SetDNNBlob([]byte) error
}
type unsupportedDREDControl interface {
SetDREDDuration(int) error
DREDDuration() (int, error)
}
type unsupportedOSCEBWEControl interface {
SetOSCEBWE(bool) error
OSCEBWE() (bool, error)
}
type ignoreExtensionsControl interface {
SetIgnoreExtensions(bool)
IgnoreExtensions() bool
Reset()
}
type lookaheadCase struct {
name string
sampleRate int
application Application
want int
}
type restrictedApplicationCase struct {
name string
application Application
wantMode encodercore.Mode
wantLowDelay bool
wantBandwidth Bandwidth
wantLookahead int
}
func assertOptionalEncoderControls(t *testing.T, enc optionalEncoderControl) {
t.Helper()
if err := enc.SetDNNBlob(nil); !errors.Is(err, ErrInvalidArgument) {
t.Fatalf("SetDNNBlob(nil) error=%v want=%v", err, ErrInvalidArgument)
}
if err := enc.SetDNNBlob([]byte{1, 2, 3}); !errors.Is(err, ErrInvalidArgument) {
t.Fatalf("SetDNNBlob(invalid) error=%v want=%v", err, ErrInvalidArgument)
}
if err := enc.SetDNNBlob(makeFramedButIncompatibleTestDNNBlob()); !errors.Is(err, ErrInvalidArgument) {
t.Fatalf("SetDNNBlob(incompatible) error=%v want=%v", err, ErrInvalidArgument)
}
if err := enc.SetDNNBlob(makeValidDecoderTestDNNBlob()); !errors.Is(err, ErrInvalidArgument) {
t.Fatalf("SetDNNBlob(decoder_blob) error=%v want=%v", err, ErrInvalidArgument)
}
if err := enc.SetDNNBlob(makeValidEncoderTestDNNBlob()); err != nil {
t.Fatalf("SetDNNBlob(encoder_blob) error=%v want=nil", err)
}
}
func assertUnsupportedDREDControl(t *testing.T, enc unsupportedDREDControl) {
t.Helper()
if err := enc.SetDREDDuration(2); !errors.Is(err, ErrUnsupportedExtension) {
t.Fatalf("SetDREDDuration error=%v want=%v", err, ErrUnsupportedExtension)
}
if got, err := enc.DREDDuration(); !errors.Is(err, ErrUnsupportedExtension) || got != 0 {
t.Fatalf("DREDDuration()=(%d,%v) want=(0,%v)", got, err, ErrUnsupportedExtension)
}
}
func assertWorkingDREDControl(t *testing.T, enc unsupportedDREDControl) {
t.Helper()
if err := enc.SetDREDDuration(4); err != nil {
t.Fatalf("SetDREDDuration(4) error: %v", err)
}
if got, err := enc.DREDDuration(); err != nil || got != 4 {
t.Fatalf("DREDDuration()=(%d,%v) want=(4,nil)", got, err)
}
if err := enc.SetDREDDuration(-1); !errors.Is(err, ErrInvalidArgument) {
t.Fatalf("SetDREDDuration(-1) error=%v want=%v", err, ErrInvalidArgument)
}
}
func assertSupportedQEXTControl(t *testing.T, enc qextEncoderControl) {
t.Helper()
if err := enc.SetQEXT(true); err != nil {
t.Fatalf("SetQEXT(true) error: %v", err)
}
if got, err := enc.QEXT(); err != nil || !got {
t.Fatalf("QEXT()=(%v,%v) want=(true,nil)", got, err)
}
if err := enc.SetQEXT(false); err != nil {
t.Fatalf("SetQEXT(false) error: %v", err)
}
if got, err := enc.QEXT(); err != nil || got {
t.Fatalf("QEXT()=(%v,%v) want=(false,nil)", got, err)
}
}
func assertUnsupportedQEXTControl(t *testing.T, enc qextEncoderControl) {
t.Helper()
if err := enc.SetQEXT(true); !errors.Is(err, ErrUnsupportedExtension) {
t.Fatalf("SetQEXT error=%v want=%v", err, ErrUnsupportedExtension)
}
if got, err := enc.QEXT(); !errors.Is(err, ErrUnsupportedExtension) || got {
t.Fatalf("QEXT()=(%v,%v) want=(false,%v)", got, err, ErrUnsupportedExtension)
}
}
func assertOptionalDecoderControls(t *testing.T, dec optionalDecoderControl) {
t.Helper()
if err := dec.SetDNNBlob(nil); !errors.Is(err, ErrInvalidArgument) {
t.Fatalf("SetDNNBlob(nil) error=%v want=%v", err, ErrInvalidArgument)
}
if err := dec.SetDNNBlob([]byte{1, 2, 3}); !errors.Is(err, ErrInvalidArgument) {
t.Fatalf("SetDNNBlob(invalid) error=%v want=%v", err, ErrInvalidArgument)
}
if err := dec.SetDNNBlob(makeFramedButIncompatibleTestDNNBlob()); !errors.Is(err, ErrInvalidArgument) {
t.Fatalf("SetDNNBlob(incompatible) error=%v want=%v", err, ErrInvalidArgument)
}
if err := dec.SetDNNBlob(makeValidEncoderTestDNNBlob()); !errors.Is(err, ErrInvalidArgument) {
t.Fatalf("SetDNNBlob(encoder_blob) error=%v want=%v", err, ErrInvalidArgument)
}
if err := dec.SetDNNBlob(makeValidDecoderTestDNNBlob()); err != nil {
t.Fatalf("SetDNNBlob(decoder_blob) error=%v want=nil", err)
}
}
func assertUnsupportedOSCEBWEControl(t *testing.T, dec unsupportedOSCEBWEControl) {
t.Helper()
if err := dec.SetOSCEBWE(true); !errors.Is(err, ErrUnsupportedExtension) {
t.Fatalf("SetOSCEBWE error=%v want=%v", err, ErrUnsupportedExtension)
}
if got, err := dec.OSCEBWE(); !errors.Is(err, ErrUnsupportedExtension) || got {
t.Fatalf("OSCEBWE()=(%v,%v) want=(false,%v)", got, err, ErrUnsupportedExtension)
}
}
func assertWorkingOSCEBWEControl(t *testing.T, dec unsupportedOSCEBWEControl) {
t.Helper()
if err := dec.SetOSCEBWE(true); err != nil {
t.Fatalf("SetOSCEBWE(true) error: %v", err)
}
if got, err := dec.OSCEBWE(); err != nil || !got {
t.Fatalf("OSCEBWE()=(%v,%v) want=(true,nil)", got, err)
}
if err := dec.SetOSCEBWE(false); err != nil {
t.Fatalf("SetOSCEBWE(false) error: %v", err)
}
if got, err := dec.OSCEBWE(); err != nil || got {
t.Fatalf("OSCEBWE()=(%v,%v) want=(false,nil)", got, err)
}
}
func TestSupportsOptionalExtension(t *testing.T) {
tests := []struct {
name string
ext OptionalExtension
want bool
}{
{name: "dred", ext: OptionalExtensionDRED, want: false},
{name: "dnn_blob", ext: OptionalExtensionDNNBlob, want: true},
{name: "qext", ext: OptionalExtensionQEXT, want: true},
{name: "osce_bwe", ext: OptionalExtensionOSCEBWE, want: false},
{name: "unknown", ext: OptionalExtension("future_ext"), want: false},
}
for _, tc := range tests {
tc := tc
t.Run(tc.name, func(t *testing.T) {
if got := SupportsOptionalExtension(tc.ext); got != tc.want {
t.Fatalf("SupportsOptionalExtension(%q)=%v want %v", tc.ext, got, tc.want)
}
})
}
}
func appendTestBlobRecord(dst []byte, name string, typ int32, payloadSize int) []byte {
const headerSize = 64
blockSize := ((payloadSize + headerSize - 1) / headerSize) * headerSize
out := make([]byte, headerSize+blockSize)
copy(out[:4], []byte("DNNw"))
binary.LittleEndian.PutUint32(out[8:12], uint32(typ))
binary.LittleEndian.PutUint32(out[12:16], uint32(payloadSize))
binary.LittleEndian.PutUint32(out[16:20], uint32(blockSize))
copy(out[20:63], []byte(name))
out[63] = 0
return append(dst, out...)
}
func appendTestBlobRecordWithPayload(dst []byte, name string, typ int32, payload []byte) []byte {
const headerSize = 64
blockSize := ((len(payload) + headerSize - 1) / headerSize) * headerSize
out := make([]byte, headerSize+blockSize)
copy(out[:4], []byte("DNNw"))
binary.LittleEndian.PutUint32(out[8:12], uint32(typ))
binary.LittleEndian.PutUint32(out[12:16], uint32(len(payload)))
binary.LittleEndian.PutUint32(out[16:20], uint32(blockSize))
copy(out[20:63], []byte(name))
out[63] = 0
copy(out[headerSize:], payload)
return append(dst, out...)
}
func encodeInt32s(values []int32) []byte {
out := make([]byte, 4*len(values))
for i, v := range values {
binary.LittleEndian.PutUint32(out[i*4:i*4+4], uint32(v))
}
return out
}
type testBlobRecordSpec struct {
typ int32
size int
}
func addLinearLayerTestBlobSpec(specs map[string]testBlobRecordSpec, spec lpcnetplc.LinearLayerSpec) {
if spec.Bias != "" {
specs[spec.Bias] = testBlobRecordSpec{typ: dnnblob.TypeFloat, size: 4 * spec.NbOutputs}
}
if spec.Subias != "" {
specs[spec.Subias] = testBlobRecordSpec{typ: dnnblob.TypeFloat, size: 4 * spec.NbOutputs}
}
if spec.Scale != "" {
specs[spec.Scale] = testBlobRecordSpec{typ: dnnblob.TypeFloat, size: 4 * spec.NbOutputs}
}
if spec.Weights != "" {
specs[spec.Weights] = testBlobRecordSpec{typ: dnnblob.TypeInt8, size: spec.NbInputs * spec.NbOutputs}
return
}
if spec.FloatWeights != "" {
specs[spec.FloatWeights] = testBlobRecordSpec{typ: dnnblob.TypeFloat, size: 4 * spec.NbInputs * spec.NbOutputs}
}
}
func addConv2DLayerTestBlobSpec(specs map[string]testBlobRecordSpec, spec lpcnetplc.Conv2DLayerSpec) {
if spec.Bias != "" {
specs[spec.Bias] = testBlobRecordSpec{typ: dnnblob.TypeFloat, size: 4 * spec.OutChannels}
}
if spec.FloatWeights != "" {
size := 4 * spec.InChannels * spec.OutChannels * spec.KTime * spec.KHeight
specs[spec.FloatWeights] = testBlobRecordSpec{typ: dnnblob.TypeFloat, size: size}
}
}
func makeFramedButIncompatibleTestDNNBlob() []byte {
return appendTestBlobRecord(nil, "dummy_record", 0, 4)
}
func makeValidEncoderTestDNNBlob() []byte {
specs := make(map[string]testBlobRecordSpec)
for _, spec := range lpcnetplc.PitchDNNLinearLayerSpecs() {
addLinearLayerTestBlobSpec(specs, spec)
}
for _, spec := range lpcnetplc.PitchDNNConv2DLayerSpecs() {
addConv2DLayerTestBlobSpec(specs, spec)
}
names := make([]string, 0, len(specs))
for name := range specs {
names = append(names, name)
}
sort.Strings(names)
var blob []byte
for _, name := range names {
spec := specs[name]
blob = appendTestBlobRecord(blob, name, spec.typ, spec.size)
}
for _, spec := range rdovae.EncoderLayerSpecs() {
blob = appendRDOVAEEncoderLayerRecords(blob, spec)
}
return blob
}
func appendRDOVAEEncoderLayerRecords(dst []byte, spec rdovae.LinearLayerSpec) []byte {
totalBlocks := 0
if spec.Bias != "" {
dst = appendTestBlobRecord(dst, spec.Bias, dnnblob.TypeFloat, 4*spec.NbOutputs)
}
if spec.Subias != "" {
dst = appendTestBlobRecord(dst, spec.Subias, dnnblob.TypeFloat, 4*spec.NbOutputs)
}
if spec.WeightsIdx != "" {
idx := make([]int32, 0, 2*(spec.NbOutputs/8))
for i := 0; i < spec.NbOutputs; i += 8 {
idx = append(idx, 1, 0)
totalBlocks++
}
dst = appendTestBlobRecordWithPayload(dst, spec.WeightsIdx, dnnblob.TypeInt, encodeInt32s(idx))
}
if spec.Weights != "" {
size := spec.NbInputs * spec.NbOutputs
if totalBlocks > 0 {
size = rdovae.SparseBlockSize * totalBlocks
}
dst = appendTestBlobRecord(dst, spec.Weights, dnnblob.TypeInt8, size)
dst = appendTestBlobRecord(dst, spec.Scale, dnnblob.TypeFloat, 4*spec.NbOutputs)
}
if spec.FloatWeights != "" {
size := spec.NbInputs * spec.NbOutputs
if totalBlocks > 0 {
size = rdovae.SparseBlockSize * totalBlocks
}
dst = appendTestBlobRecord(dst, spec.FloatWeights, dnnblob.TypeFloat, 4*size)
}
return dst
}
func makeValidDecoderTestDNNBlob() []byte {
specs := make(map[string]testBlobRecordSpec)
for _, name := range dnnblob.RequiredDecoderControlRecordNames(false) {
specs[name] = testBlobRecordSpec{typ: dnnblob.TypeFloat, size: 4}
}
for _, spec := range lpcnetplc.PitchDNNLinearLayerSpecs() {
addLinearLayerTestBlobSpec(specs, spec)
}
for _, spec := range lpcnetplc.PitchDNNConv2DLayerSpecs() {
addConv2DLayerTestBlobSpec(specs, spec)
}
for _, spec := range lpcnetplc.ModelLayerSpecs() {
addLinearLayerTestBlobSpec(specs, spec)
}
for _, spec := range lpcnetplc.FARGANModelLayerSpecs() {
addLinearLayerTestBlobSpec(specs, spec)
}
names := make([]string, 0, len(specs))
for name := range specs {
names = append(names, name)
}
sort.Strings(names)
var blob []byte
for _, name := range names {
spec := specs[name]
blob = appendTestBlobRecord(blob, name, spec.typ, spec.size)
}
return blob
}
func TestValidEncoderTestDNNBlobShape(t *testing.T) {
blob := makeValidEncoderTestDNNBlob()
if string(blob[:4]) != "DNNw" {
t.Fatalf("magic=%q want DNNw", string(blob[:4]))
}
for _, name := range dnnblob.RequiredEncoderControlRecordNames() {
if !strings.Contains(string(blob), name) {
t.Fatalf("missing record name %q", name)
}
}
}
func TestValidDecoderTestDNNBlobShape(t *testing.T) {
blob := makeValidDecoderTestDNNBlob()
if string(blob[:4]) != "DNNw" {
t.Fatalf("magic=%q want DNNw", string(blob[:4]))
}
for _, name := range dnnblob.RequiredDecoderControlRecordNames(false) {
if !strings.Contains(string(blob), name) {
t.Fatalf("missing record name %q", name)
}
}
}
func TestEncoderSetDNNBlobRetainedAcrossReset(t *testing.T) {
enc := mustNewTestEncoder(t, 48000, 2, ApplicationAudio)
if err := enc.SetDNNBlob(makeValidEncoderTestDNNBlob()); err != nil {
t.Fatalf("SetDNNBlob error: %v", err)
}
if enc.dnnBlob == nil {
t.Fatal("wrapper dnnBlob=nil want non-nil")
}
enc.Reset()
if enc.dnnBlob == nil {
t.Fatal("wrapper dnnBlob cleared by Reset")
}
}
func TestDecoderSetDNNBlobRetainedAcrossReset(t *testing.T) {
dec := mustNewTestDecoder(t, 16000, 1)
if err := dec.SetDNNBlob(makeValidDecoderTestDNNBlob()); err != nil {
t.Fatalf("SetDNNBlob error: %v", err)
}
if dec.dnnBlob == nil {
t.Fatal("wrapper dnnBlob=nil want non-nil")
}
if !dec.pitchDNNLoaded || !dec.plcModelLoaded || !dec.farganModelLoaded {
t.Fatal("decoder retained DNN model flags not armed from validated blob")
}
if dec.dredState() != nil {
t.Fatalf("decoder eagerly allocated DRED sidecar on SetDNNBlob: %+v", dec.dredState())
}
if !dec.dredNeuralConcealmentReady() {
t.Fatal("decoder failed to lazily materialize neural concealment runtime")
}
if !requireDecoderDREDState(t, dec).dredAnalysis.Loaded() || !requireDecoderDREDState(t, dec).dredPredictor.Loaded() || !requireDecoderDREDState(t, dec).dredFARGAN.Loaded() {
t.Fatal("decoder runtime models not loaded after lazy materialization")
}
dec.Reset()
if dec.dnnBlob == nil {
t.Fatal("wrapper dnnBlob cleared by Reset")
}
if !dec.pitchDNNLoaded || !dec.plcModelLoaded || !dec.farganModelLoaded {
t.Fatal("decoder retained DNN model flags cleared by Reset")
}
if !dec.dredNeuralConcealmentReady() {
t.Fatal("decoder failed to rematerialize neural concealment runtime after Reset")
}
if !requireDecoderDREDState(t, dec).dredAnalysis.Loaded() || !requireDecoderDREDState(t, dec).dredPredictor.Loaded() || !requireDecoderDREDState(t, dec).dredFARGAN.Loaded() {
t.Fatal("decoder runtime models cleared by Reset")
}
}
func TestDecoderSetDNNBlobUnsupportedConfigStaysDormant(t *testing.T) {
dec := mustNewTestDecoder(t, 48000, 2)
if err := dec.SetDNNBlob(makeValidDecoderTestDNNBlob()); err != nil {
t.Fatalf("SetDNNBlob error: %v", err)
}
if dec.dnnBlob == nil {
t.Fatal("wrapper dnnBlob=nil want non-nil")
}
if dec.dredState() != nil {
t.Fatalf("unsupported stereo config eagerly allocated DRED sidecar: %+v", dec.dredState())
}
dec.Reset()
if dec.dnnBlob == nil {
t.Fatal("wrapper dnnBlob cleared by Reset")
}
if dec.dredState() != nil {
t.Fatalf("unsupported stereo config awakened DRED sidecar after Reset: %+v", dec.dredState())
}
}
func TestMultistreamEncoderSetDNNBlobRetainedAcrossReset(t *testing.T) {
enc := mustNewDefaultMultistreamEncoder(t, 48000, 2, ApplicationAudio)
if err := enc.SetDNNBlob(makeValidEncoderTestDNNBlob()); err != nil {
t.Fatalf("SetDNNBlob error: %v", err)
}
if enc.dnnBlob == nil {
t.Fatal("wrapper dnnBlob=nil want non-nil")
}
enc.Reset()
if enc.dnnBlob == nil {
t.Fatal("wrapper dnnBlob cleared by Reset")
}
}
func TestMultistreamDecoderSetDNNBlobRetainedAcrossReset(t *testing.T) {
dec := mustNewDefaultMultistreamDecoder(t, 48000, 2)
if err := dec.SetDNNBlob(makeValidDecoderTestDNNBlob()); err != nil {
t.Fatalf("SetDNNBlob error: %v", err)
}
if dec.dnnBlob == nil {
t.Fatal("wrapper dnnBlob=nil want non-nil")
}
if !dec.dec.PitchDNNLoaded() || !dec.dec.PLCModelLoaded() || !dec.dec.FARGANModelLoaded() {
t.Fatal("multistream decoder runtime models not loaded from retained DNN blob")
}
dec.Reset()
if dec.dnnBlob == nil {
t.Fatal("wrapper dnnBlob cleared by Reset")
}
if !dec.dec.PitchDNNLoaded() || !dec.dec.PLCModelLoaded() || !dec.dec.FARGANModelLoaded() {
t.Fatal("multistream decoder runtime models cleared by Reset")
}
}
func assertIgnoreExtensionsControls(t *testing.T, dec ignoreExtensionsControl) {
t.Helper()
if dec.IgnoreExtensions() {
t.Fatal("IgnoreExtensions()=true want false by default")
}
dec.SetIgnoreExtensions(true)
if !dec.IgnoreExtensions() {
t.Fatal("IgnoreExtensions()=false want true after SetIgnoreExtensions(true)")
}
dec.Reset()
if !dec.IgnoreExtensions() {
t.Fatal("IgnoreExtensions()=false want true after Reset")
}
dec.SetIgnoreExtensions(false)
if dec.IgnoreExtensions() {
t.Fatal("IgnoreExtensions()=true want false after SetIgnoreExtensions(false)")
}
}
func lookaheadTestCases() []lookaheadCase {
return []lookaheadCase{
{
name: "audio_48k",
sampleRate: 48000,
application: ApplicationAudio,
want: 48000/400 + 48000/250,
},
{
name: "voip_48k",
sampleRate: 48000,
application: ApplicationVoIP,
want: 48000/400 + 48000/250,
},
{
name: "lowdelay_48k",
sampleRate: 48000,
application: ApplicationLowDelay,
want: 48000 / 400,
},
{
name: "audio_24k",
sampleRate: 24000,
application: ApplicationAudio,
want: 24000/400 + 24000/250,
},
{
name: "lowdelay_24k",
sampleRate: 24000,
application: ApplicationLowDelay,
want: 24000 / 400,
},
}
}
func restrictedApplicationTestCases() []restrictedApplicationCase {
return []restrictedApplicationCase{
{
name: "restricted_silk",
application: ApplicationRestrictedSilk,
wantMode: encodercore.ModeSILK,
wantLowDelay: false,
wantBandwidth: BandwidthWideband,
wantLookahead: 48000/400 + 48000/250,
},
{
name: "restricted_celt",
application: ApplicationRestrictedCelt,
wantMode: encodercore.ModeCELT,
wantLowDelay: true,
wantBandwidth: BandwidthFullband,
wantLookahead: 48000 / 400,
},
}
}
func assertApplicationLockAfterEncode(
t *testing.T,
currentApplication func() Application,
setApplication func(Application) error,
reset func(),
encodeOnce func() error,
changeTo Application,
resetTo Application,
) {
t.Helper()
if err := encodeOnce(); err != nil {
t.Fatalf("encode before application lock test error: %v", err)
}
if err := setApplication(changeTo); err != ErrInvalidApplication {
t.Fatalf("SetApplication(change after encode) error=%v want=%v", err, ErrInvalidApplication)
}
if err := setApplication(currentApplication()); err != nil {
t.Fatalf("SetApplication(same after encode) error: %v", err)
}
reset()
if err := setApplication(resetTo); err != nil {
t.Fatalf("SetApplication(after reset) error: %v", err)
}
}
func assertLookaheadUpdatesBeforeEncode(
t *testing.T,
lookahead func() int,
setApplication func(Application) error,
) {
t.Helper()
if got, want := lookahead(), 48000/400+48000/250; got != want {
t.Fatalf("Lookahead(audio) = %d, want %d", got, want)
}
if err := setApplication(ApplicationLowDelay); err != nil {
t.Fatalf("SetApplication(LowDelay) error: %v", err)
}
if got, want := lookahead(), 48000/400; got != want {
t.Fatalf("Lookahead(lowdelay) = %d, want %d", got, want)
}
if err := setApplication(ApplicationAudio); err != nil {
t.Fatalf("SetApplication(Audio) error: %v", err)
}
if got, want := lookahead(), 48000/400+48000/250; got != want {
t.Fatalf("Lookahead(audio after reset) = %d, want %d", got, want)
}
}