-
Notifications
You must be signed in to change notification settings - Fork 3
/
OpusEncoder.cs
287 lines (247 loc) · 9.04 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
279
280
281
282
283
284
285
286
287
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using POpusCodec.Enums;
namespace POpusCodec
{
public class OpusEncoder : IDisposable
{
public const int BitrateMax = -1;
private IntPtr _handle = IntPtr.Zero;
private string _version = string.Empty;
private const int RecommendedMaxPacketSize = 4000;
private int _frameSizePerChannel = 960;
private SamplingRate _inputSamplingRate = SamplingRate.Sampling48000;
private Channels _inputChannels = Channels.Stereo;
public SamplingRate InputSamplingRate
{
get
{
return _inputSamplingRate;
}
}
public Channels InputChannels
{
get
{
return _inputChannels;
}
}
private byte[] writePacket = new byte[RecommendedMaxPacketSize];
public string Version
{
get
{
return _version;
}
}
private Delay _encoderDelay = Delay.Delay20ms;
/// <summary>
/// Using a duration of less than 10 ms will prevent the encoder from using the LPC or hybrid modes.
/// </summary>
public Delay EncoderDelay
{
set
{
_encoderDelay = value;
_frameSizePerChannel = (int)((((int)_inputSamplingRate) / 1000) * ((decimal)_encoderDelay) / 2);
}
get
{
return _encoderDelay;
}
}
public int FrameSizePerChannel
{
get
{
return _frameSizePerChannel;
}
}
public int Bitrate
{
get
{
return Wrapper.get_opus_encoder_ctl(_handle, OpusCtlGetRequest.Bitrate);
}
set
{
Wrapper.set_opus_encoder_ctl(_handle, OpusCtlSetRequest.Bitrate, value);
}
}
public Bandwidth MaxBandwidth
{
get
{
return (Bandwidth)Wrapper.get_opus_encoder_ctl(_handle, OpusCtlGetRequest.MaxBandwidth);
}
set
{
Wrapper.set_opus_encoder_ctl(_handle, OpusCtlSetRequest.MaxBandwidth, (int)value);
}
}
public Complexity Complexity
{
get
{
return (Complexity)Wrapper.get_opus_encoder_ctl(_handle, OpusCtlGetRequest.Complexity);
}
set
{
Wrapper.set_opus_encoder_ctl(_handle, OpusCtlSetRequest.Complexity, (int)value);
}
}
public int ExpectedPacketLossPercentage
{
get
{
return Wrapper.get_opus_encoder_ctl(_handle, OpusCtlGetRequest.PacketLossPercentage);
}
set
{
Wrapper.set_opus_encoder_ctl(_handle, OpusCtlSetRequest.PacketLossPercentage, value);
}
}
public SignalHint SignalHint
{
get
{
return (SignalHint)Wrapper.get_opus_encoder_ctl(_handle, OpusCtlGetRequest.Signal);
}
set
{
Wrapper.set_opus_encoder_ctl(_handle, OpusCtlSetRequest.Signal, (int)value);
}
}
public ForceChannels ForceChannels
{
get
{
return (ForceChannels)Wrapper.get_opus_encoder_ctl(_handle, OpusCtlGetRequest.ForceChannels);
}
set
{
Wrapper.set_opus_encoder_ctl(_handle, OpusCtlSetRequest.ForceChannels, (int)value);
}
}
public bool UseInbandFEC
{
get
{
return Wrapper.get_opus_encoder_ctl(_handle, OpusCtlGetRequest.InbandFec) == 1;
}
set
{
Wrapper.set_opus_encoder_ctl(_handle, OpusCtlSetRequest.InbandFec, value ? 1 : 0);
}
}
public bool UseUnconstrainedVBR
{
get
{
return Wrapper.get_opus_encoder_ctl(_handle, OpusCtlGetRequest.VBRConstraint) == 0;
}
set
{
Wrapper.set_opus_encoder_ctl(_handle, OpusCtlSetRequest.VBRConstraint, value ? 0 : 1);
}
}
public bool DtxEnabled
{
get
{
return Wrapper.get_opus_encoder_ctl(_handle, OpusCtlGetRequest.Dtx) == 1;
}
set
{
Wrapper.set_opus_encoder_ctl(_handle, OpusCtlSetRequest.Dtx, value ? 1 : 0);
}
}
public OpusEncoder(SamplingRate inputSamplingRateHz, Channels numChannels)
: this(inputSamplingRateHz, numChannels, OpusApplicationType.Audio, Delay.Delay20ms)
{ }
public OpusEncoder(SamplingRate inputSamplingRateHz, Channels numChannels, OpusApplicationType applicationType)
: this(inputSamplingRateHz, numChannels, applicationType, Delay.Delay20ms)
{ }
public OpusEncoder(SamplingRate inputSamplingRateHz, Channels numChannels, OpusApplicationType applicationType, Delay encoderDelay)
{
if ((inputSamplingRateHz != SamplingRate.Sampling08000)
&& (inputSamplingRateHz != SamplingRate.Sampling12000)
&& (inputSamplingRateHz != SamplingRate.Sampling16000)
&& (inputSamplingRateHz != SamplingRate.Sampling24000)
&& (inputSamplingRateHz != SamplingRate.Sampling48000))
{
throw new ArgumentOutOfRangeException("inputSamplingRateHz", "Must use one of the pre-defined sampling rates");
}
if ((numChannels != Channels.Mono)
&& (numChannels != Channels.Stereo))
{
throw new ArgumentOutOfRangeException("numChannels", "Must be Mono or Stereo");
}
if ((applicationType != OpusApplicationType.Audio)
&& (applicationType != OpusApplicationType.RestrictedLowDelay)
&& (applicationType != OpusApplicationType.Voip))
{
throw new ArgumentOutOfRangeException("applicationType", "Must use one of the pre-defined application types");
}
if ((encoderDelay != Delay.Delay10ms)
&& (encoderDelay != Delay.Delay20ms)
&& (encoderDelay != Delay.Delay2dot5ms)
&& (encoderDelay != Delay.Delay40ms)
&& (encoderDelay != Delay.Delay5ms)
&& (encoderDelay != Delay.Delay60ms))
{
throw new ArgumentOutOfRangeException("encoderDelay", "Must use one of the pre-defined delay values");
}
_inputSamplingRate = inputSamplingRateHz;
_inputChannels = numChannels;
_handle = Wrapper.opus_encoder_create(inputSamplingRateHz, numChannels, applicationType);
_version = Wrapper.opus_get_version();
if (_handle == IntPtr.Zero)
{
throw new OpusException(OpusStatusCode.AllocFail, "Memory was not allocated for the encoder");
}
EncoderDelay = encoderDelay;
}
public byte[] Encode(short[] pcmSamples) //pcmSamples: length is frame_size*channels*sizeof(short)
{
int size = Wrapper.opus_encode(_handle, pcmSamples, _frameSizePerChannel, writePacket);
if (size <= 1) //DTX. Negative already handled at this point
return new byte[] { };
byte[] encodedData = new byte[size];
Buffer.BlockCopy(writePacket, 0, encodedData, 0, encodedData.Length);
return encodedData;
}
public byte[] Encode(float[] pcmSamples)
{
int size = Wrapper.opus_encode(_handle, pcmSamples, _frameSizePerChannel, writePacket);
if (size <= 1) //DTX. Negative already handled at this point
return new byte[] { };
byte[] encodedData = new byte[size];
Buffer.BlockCopy(writePacket, 0, encodedData, 0, encodedData.Length);
return encodedData;
}
private bool disposed = false;
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (disposed)
return;
if (_handle != IntPtr.Zero)
{
Wrapper.opus_encoder_destroy(_handle);
_handle = IntPtr.Zero;
}
disposed = true;
}
~OpusEncoder()
{
Dispose(false);
}
}
}