-
Notifications
You must be signed in to change notification settings - Fork 3
/
OpusWrapper.cs
248 lines (187 loc) · 9.21 KB
/
OpusWrapper.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using POpusCodec.Enums;
namespace POpusCodec
{
internal class Wrapper
{
[DllImport("libopus.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
private static extern int opus_encoder_get_size(Channels channels);
[DllImport("libopus.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
private static extern OpusStatusCode opus_encoder_init(IntPtr st, SamplingRate Fs, Channels channels, OpusApplicationType application);
[DllImport("libopus.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
private static extern IntPtr opus_get_version_string();
public static string opus_get_version()
{
return Marshal.PtrToStringAuto(opus_get_version_string());
}
public static IntPtr opus_encoder_create(SamplingRate Fs, Channels channels, OpusApplicationType application)
{
int size = Wrapper.opus_encoder_get_size(channels);
IntPtr ptr = Marshal.AllocHGlobal(size);
OpusStatusCode statusCode = Wrapper.opus_encoder_init(ptr, Fs, channels, application);
try
{
HandleStatusCode(statusCode);
}
catch (Exception ex)
{
if (ptr != IntPtr.Zero)
{
Wrapper.opus_encoder_destroy(ptr);
ptr = IntPtr.Zero;
}
throw ex;
}
return ptr;
}
[DllImport("libopus.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
private static extern int opus_encode(IntPtr st, short[] pcm, int frame_size, byte[] data, int max_data_bytes);
public static int opus_encode(IntPtr st, short[] pcm, int frame_size, byte[] data)
{
if (st == IntPtr.Zero)
throw new ObjectDisposedException("OpusEncoder");
int payloadLength = opus_encode(st, pcm, frame_size, data, data.Length);
if (payloadLength <= 0)
{
HandleStatusCode((OpusStatusCode)payloadLength);
}
return payloadLength;
}
[DllImport("libopus.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
private static extern int opus_encode_float(IntPtr st, float[] pcm, int frame_size, byte[] data, int max_data_bytes);
public static int opus_encode(IntPtr st, float[] pcm, int frame_size, byte[] data)
{
if (st == IntPtr.Zero)
throw new ObjectDisposedException("OpusEncoder");
int payloadLength = opus_encode_float(st, pcm, frame_size, data, data.Length);
if (payloadLength <= 0)
{
HandleStatusCode((OpusStatusCode)payloadLength);
}
return payloadLength;
}
public static void opus_encoder_destroy(IntPtr st)
{
Marshal.FreeHGlobal(st);
}
[DllImport("libopus.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
private static extern int opus_encoder_ctl(IntPtr st, OpusCtlSetRequest request, int value);
[DllImport("libopus.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
private static extern int opus_encoder_ctl(IntPtr st, OpusCtlGetRequest request, ref int value);
public static int get_opus_encoder_ctl(IntPtr st, OpusCtlGetRequest request)
{
if (st == IntPtr.Zero)
throw new ObjectDisposedException("OpusEncoder");
int value = 0;
OpusStatusCode statusCode = (OpusStatusCode)opus_encoder_ctl(st, request, ref value);
HandleStatusCode(statusCode);
return value;
}
public static void set_opus_encoder_ctl(IntPtr st, OpusCtlSetRequest request, int value)
{
if (st == IntPtr.Zero)
throw new ObjectDisposedException("OpusEncoder");
OpusStatusCode statusCode = (OpusStatusCode)opus_encoder_ctl(st, request, value);
HandleStatusCode(statusCode);
}
[DllImport("libopus.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
private static extern int opus_decoder_get_size(Channels channels);
[DllImport("libopus.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
private static extern OpusStatusCode opus_decoder_init(IntPtr st, SamplingRate Fs, Channels channels);
public static IntPtr opus_decoder_create(SamplingRate Fs, Channels channels)
{
int size = Wrapper.opus_decoder_get_size(channels);
IntPtr ptr = Marshal.AllocHGlobal(size);
OpusStatusCode statusCode = Wrapper.opus_decoder_init(ptr, Fs, channels);
try
{
HandleStatusCode(statusCode);
}
catch (Exception ex)
{
if (ptr != IntPtr.Zero)
{
Wrapper.opus_decoder_destroy(ptr);
ptr = IntPtr.Zero;
}
throw ex;
}
return ptr;
}
public static void opus_decoder_destroy(IntPtr st)
{
Marshal.FreeHGlobal(st);
}
[DllImport("libopus.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
private static extern int opus_decode(IntPtr st, byte[] data, int len, short[] pcm, int frame_size, int decode_fec);
[DllImport("libopus.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
private static extern int opus_decode_float(IntPtr st, byte[] data, int len, float[] pcm, int frame_size, int decode_fec);
[DllImport("libopus.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
private static extern int opus_decode(IntPtr st, IntPtr data, int len, short[] pcm, int frame_size, int decode_fec);
[DllImport("libopus.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
private static extern int opus_decode_float(IntPtr st, IntPtr data, int len, float[] pcm, int frame_size, int decode_fec);
public static int opus_decode(IntPtr st, byte[] data, short[] pcm, int decode_fec, int channels)
{
if (st == IntPtr.Zero)
throw new ObjectDisposedException("OpusDecoder");
int numSamplesDecoded = 0;
if (data != null)
{
numSamplesDecoded = opus_decode(st, data, data.Length, pcm, pcm.Length / channels, decode_fec);
}
else
{
numSamplesDecoded = opus_decode(st, IntPtr.Zero, 0, pcm, pcm.Length / channels, decode_fec);
}
if (numSamplesDecoded == (int)OpusStatusCode.InvalidPacket)
return 0;
if (numSamplesDecoded <= 0)
{
HandleStatusCode((OpusStatusCode)numSamplesDecoded);
}
return numSamplesDecoded;
}
public static int opus_decode(IntPtr st, byte[] data, float[] pcm, int decode_fec, int channels)
{
if (st == IntPtr.Zero)
throw new ObjectDisposedException("OpusDecoder");
int numSamplesDecoded = 0;
if (data != null)
{
numSamplesDecoded = opus_decode_float(st, data, data.Length, pcm, pcm.Length / channels, decode_fec);
}
else
{
numSamplesDecoded = opus_decode_float(st, IntPtr.Zero, 0, pcm, pcm.Length / channels, decode_fec);
}
if (numSamplesDecoded == (int)OpusStatusCode.InvalidPacket)
return 0;
if (numSamplesDecoded <= 0)
{
HandleStatusCode((OpusStatusCode)numSamplesDecoded);
}
return numSamplesDecoded;
}
[DllImport("libopus.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
public static extern int opus_packet_get_bandwidth(byte[] data);
[DllImport("libopus.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
public static extern int opus_packet_get_nb_channels(byte[] data);
[DllImport("libopus.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
private static extern IntPtr opus_strerror(OpusStatusCode error);
private static string opus_error(OpusStatusCode error)
{
return Marshal.PtrToStringAuto(opus_strerror(error));
}
private static void HandleStatusCode(OpusStatusCode statusCode)
{
if (statusCode != OpusStatusCode.OK)
{
throw new OpusException(statusCode, opus_error(statusCode));
}
}
}
}