-
Notifications
You must be signed in to change notification settings - Fork 0
/
encoder_test.go
300 lines (252 loc) · 8.38 KB
/
encoder_test.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
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
package caress
import (
"testing"
"github.com/tkmn0/caress/opus"
)
func TestEncoderInit(t *testing.T) {
enc, err := NewEncoder(48000, 1, ApplicationVoip)
if err != nil || enc == nil {
t.Errorf("Error creating new encoder: %v", err)
}
enc, err = NewEncoder(12345, 1, ApplicationVoip)
if err == nil || enc != nil {
t.Errorf("Expected error for illegal samplerate 12345")
}
}
func TestEncoderUnitialized(t *testing.T) {
var enc Encoder
_, err := enc.Encode(nil, nil)
if err != ErrorUnInitialized {
t.Errorf("Encode Expected \"uninitialized error\" error: %v", err)
}
_, err = enc.EncodeFloat(nil, nil)
if err != ErrorUnInitialized {
t.Errorf("EncodeFloat Expected \"uninitialized error\" error: %v", err)
}
err = enc.Setbitrate(600)
if err != ErrorUnInitialized {
t.Errorf("SetBitrate Expected \"uninitialized error\" error: %v", err)
}
_, err = enc.GetBitrate()
if err != ErrorUnInitialized {
t.Errorf("SetBitrate Expected \"uninitialized error\" error: %v", err)
}
err = enc.SetComplexity(10)
if err != ErrorUnInitialized {
t.Errorf("SetComplexity Expected \"uninitialized error\" error: %v", err)
}
_, err = enc.GetComplexity()
if err != ErrorUnInitialized {
t.Errorf("GetComplexity Expected \"uninitialized error\" error: %v", err)
}
err = enc.SetSignal(SignalAuto)
if err != ErrorUnInitialized {
t.Errorf("SetSignal Expected \"uninitialized error\" error: %v", err)
}
_, err = enc.GetSignal()
if err != ErrorUnInitialized {
t.Errorf("GetSignal Expected \"uninitialized error\" error: %v", err)
}
err = enc.SetInBandFEC(true)
if err != ErrorUnInitialized {
t.Errorf("SetInBandFEC Expected \"uninitialized error\" error: %v", err)
}
_, err = enc.GetInBandFEC()
if err != ErrorUnInitialized {
t.Errorf("GetInBandFEC Expected \"uninitialized error\" error: %v", err)
}
err = enc.SetPacketLossPercentage(30)
if err != ErrorUnInitialized {
t.Errorf("SetPacketLossPercentage Expected \"uninitialized error\" error: %v", err)
}
_, err = enc.GetPacketLossPercentage()
if err != ErrorUnInitialized {
t.Errorf("GetPacketLossPercentage Expected \"uninitialized error\" error: %v", err)
}
}
func TestNoDataSupplied(t *testing.T) {
enc, err := NewEncoder(48000, 1, ApplicationVoip)
if err != nil {
t.Errorf("encoder create error (this is expected to be passed, check your environment)")
}
buffer := make([]byte, 1024)
_, err = enc.Encode(nil, buffer)
if err != ErrorNoDataSupplied {
t.Errorf("Expected \"no data supplied error\" error: %v", err)
}
_, err = enc.EncodeFloat(nil, buffer)
if err != ErrorNoDataSupplied {
t.Errorf("Expected \"no data supplied error\" error: %v", err)
}
}
func TestNoTargetBuffer(t *testing.T) {
enc, err := NewEncoder(48000, 1, ApplicationVoip)
if err != nil {
t.Errorf("encoder create error (this is expected to be passed, check your environment)")
}
pcmInt16 := make([]int16, 1024)
_, err = enc.Encode(pcmInt16, nil)
if err != ErrorNoTargetbuffer {
t.Errorf("Expected \"no target buffer error\" error: %v", err)
}
pcmFloat32 := make([]float32, 1024)
_, err = enc.EncodeFloat(pcmFloat32, nil)
if err != ErrorNoTargetbuffer {
t.Errorf("Expected \"no target buffer error\" error: %v", err)
}
}
func TestInvalidPcmSizeEncode(t *testing.T) {
enc, _ := NewEncoder(48000, 2, ApplicationVoip)
pcm := make([]int16, 255)
buffer := make([]byte, 256)
_, err := enc.Encode(pcm, buffer)
if err != ErrorSuppliedDataSize {
t.Errorf("Expected \"input buffer length must be multiple of channels\" error: %v", err)
}
pcmFloat := make([]float32, 255)
_, err = enc.EncodeFloat(pcmFloat, buffer)
if err != ErrorSuppliedDataSize {
t.Errorf("Expected \"input buffer length must be multiple of channels\" error: %v", err)
}
}
func TestEncoderBitrate(t *testing.T) {
enc, err := NewEncoder(48000, 1, ApplicationVoip)
if err != nil {
t.Errorf("encoder create error (this is expected to be passed, check your environment)")
}
var targetBitrate int32 = 1000
err = enc.Setbitrate(targetBitrate)
if err != nil && err != ErrorSetBitrate {
t.Errorf("Expected \"set bitrate error\" error: %v", err)
}
bitrate, err := enc.GetBitrate()
if err != nil {
t.Errorf("get bit rate error: %v", err)
}
if bitrate != targetBitrate {
t.Errorf("Expected bitrate is %v, but result is %v", targetBitrate, bitrate)
}
}
func TestEncoderSetInvalidBitrate(t *testing.T) {
enc, err := NewEncoder(48000, 1, ApplicationVoip)
if err != nil {
t.Errorf("encoder create error (this is expected to be passed, check your environment)")
}
err = enc.Setbitrate(opus.MinBitrate - 100)
if err != ErrorSetBitrateInvalidSize {
t.Errorf("Expected %v error: %v", ErrorSetBitrateInvalidSize, err)
}
err = enc.Setbitrate(opus.MaxBitrate + 100)
if err != ErrorSetBitrateInvalidSize {
t.Errorf("Expected %v error: %v", ErrorSetBitrateInvalidSize, err)
}
}
func TestEncoderComplexity(t *testing.T) {
enc, err := NewEncoder(48000, 1, ApplicationVoip)
if err != nil {
t.Errorf("encoder create error (this is expected to be passed, check your environment)")
}
var targetComplexity int32 = 7
err = enc.SetComplexity(targetComplexity)
if err != nil {
t.Errorf("encoder set complexity error: %v", err)
}
c, err := enc.GetComplexity()
if err != nil {
t.Errorf("encoder get complexity error: %v", err)
}
if c != targetComplexity {
t.Errorf("Expected complexity is %v, but result is %v", targetComplexity, c)
}
}
func TestEncoderSetInvalidComplexity(t *testing.T) {
enc, err := NewEncoder(48000, 1, ApplicationVoip)
if err != nil {
t.Errorf("encoder create error (this is expected to be passed, check your environment)")
}
err = enc.SetComplexity(opus.MaxComplexity + 1)
if err != ErrorSetComplexityInvalidSize {
t.Errorf("Expected %v error: %v", ErrorSetComplexityInvalidSize, err)
}
err = enc.SetComplexity(opus.MinComplexity - 1)
if err != ErrorSetComplexityInvalidSize {
t.Errorf("Expected %v error: %v", ErrorSetComplexityInvalidSize, err)
}
}
func TestEncoderSetSignal(t *testing.T) {
enc, err := NewEncoder(48000, 1, ApplicationVoip)
if err != nil {
t.Errorf("encoder create error (this is expected to be passed, check your environment)")
}
err = enc.SetSignal(SignalVoice)
if err != nil {
t.Errorf("Encoder set signal error")
}
signal, err := enc.GetSignal()
if err != nil {
t.Errorf("Encoder get signal error")
}
if signal != SignalVoice {
t.Errorf("Expected signal value is %v, but result is %v", SignalVoice, signal)
}
}
func TestEncoderSetSignalInvalidValue(t *testing.T) {
enc, err := NewEncoder(48000, 1, ApplicationVoip)
if err != nil {
t.Errorf("encoder create error (this is expected to be passed, check your environment)")
}
err = enc.SetSignal(6000)
if err == nil || err != ErrorSetSignalInvalidValue {
t.Errorf("Expected error is %v, but error is %v", ErrorSetSignalInvalidValue, err)
}
}
func TestEncoderSetInBandFEC(t *testing.T) {
enc, err := NewEncoder(48000, 1, ApplicationVoip)
if err != nil {
t.Errorf("encoder create error (this is expected to be passed, check your environment)")
}
enabled := true
err = enc.SetInBandFEC(enabled)
if err != nil {
t.Errorf("set inband fec error")
}
result, err := enc.GetInBandFEC()
if err != nil {
t.Errorf("get inband fec error")
}
if enabled != result {
t.Errorf("set iband fec value is %v, but result is %v", enabled, result)
}
}
func TestEncoderSetPacketLossPercentage(t *testing.T) {
enc, err := NewEncoder(48000, 1, ApplicationVoip)
if err != nil {
t.Errorf("encoder create error (this is expected to be passed, check your environment)")
}
var perc int32 = 70
err = enc.SetPacketLossPercentage(perc)
if err != nil {
t.Errorf("set packet loss percentage error")
}
result, err := enc.GetPacketLossPercentage()
if err != nil {
t.Errorf("get packet loss percentage error")
}
if perc != result {
t.Errorf("set to %v percentage but result is %v percentage", perc, result)
}
}
func TestEncoderSetPakcetLossInvalidPercentage(t *testing.T) {
enc, err := NewEncoder(48000, 1, ApplicationVoip)
if err != nil {
t.Errorf("encoder create error (this is expected to be passed, check your environment)")
}
err = enc.SetPacketLossPercentage(120)
if err != ErrorSetPacketLossPercInvalidValue {
t.Errorf("Expected error is %v, but error is %v", ErrorSetPacketLossPercInvalidValue, err)
}
err = enc.SetPacketLossPercentage(-230)
if err != ErrorSetPacketLossPercInvalidValue {
t.Errorf("Expected error is %v, but error is %v", ErrorSetPacketLossPercInvalidValue, err)
}
}