-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOpusEncoder.cs
278 lines (236 loc) · 7.84 KB
/
OpusEncoder.cs
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
using System;
using System.Runtime.InteropServices;
using System.Threading;
namespace NuclearVOIP
{
internal class OpusEncoder: AbstractTransform<float, byte[]>
{
private const bool DECODER_TEST = false;
private readonly IntPtr encoder;
private readonly IntPtr decoder;
private bool decoder_good = true;
private readonly int frameSize;
private readonly Mutex readLock = new(); // Currently not internally reordered so locking is needed
private bool closed = false;
private float[]? leftover;
public readonly int lookahead;
public int BitRate
{
get
{
return GetCtl(LibOpus.EncoderCtl.GET_BITRATE);
}
set
{
SetCtl(LibOpus.EncoderCtl.SET_BITRATE, value);
}
}
public int PacketLoss
{
get
{
return GetCtl(LibOpus.EncoderCtl.GET_PACKET_LOSS_PERC);
}
set
{
SetCtl(LibOpus.EncoderCtl.SET_PACKET_LOSS_PERC, value);
}
}
public LibOpus.FEC FEC
{
get
{
return (LibOpus.FEC)GetCtl(LibOpus.EncoderCtl.GET_INBAND_FEC);
}
set
{
SetCtl(LibOpus.EncoderCtl.SET_INBAND_FEC, (int)value);
}
}
public bool DTX
{
get
{
return GetCtl(LibOpus.EncoderCtl.GET_DTX) == 1;
}
set
{
SetCtl(LibOpus.EncoderCtl.SET_DTX, value ? 1 : 0);
}
}
public LibOpus.Signal Signal
{
get
{
return (LibOpus.Signal)GetCtl(LibOpus.EncoderCtl.GET_SIGNAL);
}
set
{
SetCtl(LibOpus.EncoderCtl.SET_SIGNAL, (int)value);
}
}
public int LSB_Depth
{
get
{
return GetCtl(LibOpus.EncoderCtl.GET_LSB_DEPTH);
}
set
{
SetCtl(LibOpus.EncoderCtl.SET_LSB_DEPTH, value);
}
}
unsafe public OpusEncoder(int frequency)
{
frameSize = (int)(0.02 * frequency);
encoder = Marshal.AllocHGlobal(LibOpus.opus_encoder_get_size(1));
int err = LibOpus.opus_encoder_init(encoder, frequency, 1, (int)LibOpus.Modes.VOIP);
if (err != 0)
{
Marshal.FreeHGlobal(encoder);
throw new LibOpus.OpusException(err);
}
if (DECODER_TEST)
{
decoder = Marshal.AllocHGlobal(LibOpus.opus_decoder_get_size(1));
err = LibOpus.opus_decoder_init(decoder, frequency, 1);
if (err != 0)
{
Marshal.FreeHGlobal(encoder);
Marshal.FreeHGlobal(decoder);
throw new LibOpus.OpusException(err);
}
}
BitRate = 24000;
//FEC = LibOpus.FEC.AGGRESSIVE;
//DTX = true;
Signal = LibOpus.Signal.VOICE;
lookahead = GetCtl(LibOpus.EncoderCtl.GET_LOOKAHEAD);
}
~OpusEncoder()
{
Marshal.FreeHGlobal(encoder);
if (DECODER_TEST)
Marshal.FreeHGlobal(decoder);
}
private byte[][] DoEncode(StreamArgs<float> args)
{
if (!readLock.WaitOne(0))
return [];
try
{
args.Handle();
float[] rawFrames = leftover == null ? args.data : [..leftover, ..args.data];
int mod = rawFrames.Length % frameSize;
if (mod == 0)
leftover = null;
else
{
leftover = rawFrames[^mod..];
Array.Resize(ref rawFrames, rawFrames.Length - mod);
}
if (rawFrames.Length == 0)
return [];
byte[][] encoded = new byte[rawFrames.Length / frameSize][];
int offset = -frameSize;
for (int i = 0; i < encoded.Length; i++)
{
offset += frameSize;
encoded[i] = EncodeFrame(rawFrames[offset..(offset + frameSize)]);
}
return encoded;
}
finally
{
readLock.ReleaseMutex();
}
}
public void Close()
{
readLock.WaitOne();
try
{
closed = true;
if (leftover != null)
{
Array.Resize(ref leftover, frameSize); // Should never be a frame size or bigger, else would have already encoded
_Write([EncodeFrame(leftover)]);
}
}
finally
{
readLock.ReleaseMutex();
}
}
override protected byte[][] Transform(float[] samples)
{
if (closed)
throw new InvalidOperationException("OpusEncoder was closed");
return DoEncode(new(samples));
}
private unsafe byte[] EncodeFrame(float[] samples)
{
byte[] frame = new byte[4000];
int err = LibOpus.opus_encode_float(encoder, samples, frameSize, frame, 4000);
if (err < 0)
throw new LibOpus.OpusException(err);
Array.Resize(ref frame, err);
if (DECODER_TEST && decoder_good)
{
float[] decoded = new float[5760];
err = LibOpus.opus_decode_float(decoder, frame, frame.Length, decoded, 5760, 0);
if (err < 0)
{
Plugin.Logger.LogWarning("Decoder failure when verifying encoding. Disabling verification.");
decoder_good = false;
}
else
{
if (err != frameSize)
throw new ValidationException();
Array.Resize(ref decoded, err);
for (int i = 0; i < frameSize; i++)
{
float error = decoded[i] - samples[i];
if (Math.Abs(error) > 0.2)
{
ValidationException exception = new(samples[i], decoded[i]);
//throw exception;
Plugin.Logger.LogWarning($"{exception.Message} error = {error}. Disabling verification.");
decoder_good = false;
}
}
}
}
return frame;
}
private void SetCtl(LibOpus.EncoderCtl ctl, int val)
{
int err = LibOpus.opus_encoder_ctl(encoder, (int)ctl, val);
if (err != 0)
{
Marshal.FreeHGlobal(encoder);
throw new LibOpus.OpusException(err);
}
}
private unsafe int GetCtl(LibOpus.EncoderCtl ctl)
{
int err = LibOpus.opus_encoder_ctl(encoder, (int)ctl, out int result);
if (err != 0)
{
Marshal.FreeHGlobal(encoder);
throw new LibOpus.OpusException(err);
}
return result;
}
public class ValidationException: Exception
{
internal ValidationException(): base("encoder decoder length disagreement")
{
}
internal ValidationException(float encoded, float decoded): base($"encoder decoder sample disagreement ({encoded}, {decoded})")
{
}
};
}
}