-
Notifications
You must be signed in to change notification settings - Fork 76
Expand file tree
/
Copy pathnode-opus.cc
More file actions
346 lines (268 loc) · 9.96 KB
/
Copy pathnode-opus.cc
File metadata and controls
346 lines (268 loc) · 9.96 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
#include "napi.h"
#include "../deps/opus/include/opus.h"
#include "node-opus.h"
using namespace Napi;
const char* getDecodeError(int decodedSamples) {
switch(decodedSamples) {
case OPUS_BAD_ARG:
return "One or more invalid/out of range arguments";
case OPUS_BUFFER_TOO_SMALL:
return "The mode struct passed is invalid";
case OPUS_INTERNAL_ERROR:
return "An internal error was detected";
case OPUS_INVALID_PACKET:
return "The compressed data passed is corrupted";
case OPUS_UNIMPLEMENTED:
return "Invalid/unsupported request number";
case OPUS_INVALID_STATE:
return "An encoder or decoder structure is invalid or already freed.";
case OPUS_ALLOC_FAIL:
return "Memory allocation has failed";
default:
return "Unknown OPUS error";
}
}
Object NodeOpusEncoder::Init(Napi::Env env, Object exports) {
HandleScope scope(env);
Function func = DefineClass(env, "OpusEncoder", {
InstanceMethod("encode", &NodeOpusEncoder::Encode),
InstanceMethod("decode", &NodeOpusEncoder::Decode),
InstanceMethod("conceal", &NodeOpusEncoder::Conceal),
InstanceMethod("applyEncoderCTL", &NodeOpusEncoder::ApplyEncoderCTL),
InstanceMethod("applyDecoderCTL", &NodeOpusEncoder::ApplyDecoderCTL),
InstanceMethod("setBitrate", &NodeOpusEncoder::SetBitrate),
InstanceMethod("getBitrate", &NodeOpusEncoder::GetBitrate),
});
exports.Set("OpusEncoder", func);
return exports;
}
NodeOpusEncoder::NodeOpusEncoder(const CallbackInfo& args): ObjectWrap<NodeOpusEncoder>(args) {
this->encoder = nullptr;
this->decoder = nullptr;
this->outPcm = nullptr;
this->rate = 48000;
this->channels = 2;
if (args.Length() >= 1 && !args[0].IsUndefined()) {
if (!args[0].IsNumber()) {
Napi::TypeError::New(args.Env(), "Expected rate to be a number").ThrowAsJavaScriptException();
return;
}
this->rate = args[0].ToNumber().Int32Value();
}
if (args.Length() >= 2 && !args[1].IsUndefined()) {
if (!args[1].IsNumber()) {
Napi::TypeError::New(args.Env(), "Expected channels to be a number").ThrowAsJavaScriptException();
return;
}
this->channels = args[1].ToNumber().Int32Value();
}
this->application = OPUS_APPLICATION_AUDIO;
this->outPcm = new opus_int16[this->channels * MAX_FRAME_SIZE];
}
NodeOpusEncoder::~NodeOpusEncoder() {
if (this->encoder) opus_encoder_destroy(this->encoder);
if (this->decoder) opus_decoder_destroy(this->decoder);
this->encoder = nullptr;
this->decoder = nullptr;
if (this->outPcm) delete this->outPcm;
this->outPcm = nullptr;
}
int NodeOpusEncoder::EnsureEncoder() {
if (this->encoder) return 0;
int error;
this->encoder = opus_encoder_create(rate, channels, application, &error);
return error;
}
int NodeOpusEncoder::EnsureDecoder() {
if (this->decoder) return 0;
int error;
this->decoder = opus_decoder_create(rate, channels, &error);
return error;
}
Napi::Value NodeOpusEncoder::Encode(const CallbackInfo& args) {
Napi::Env env = args.Env();
if (this->EnsureEncoder() != OPUS_OK) {
Napi::Error::New(env, "Could not create encoder. Check the encoder parameters").ThrowAsJavaScriptException();
return env.Null();
}
if (args.Length() < 1) {
Napi::RangeError::New(env, "Expected 1 argument").ThrowAsJavaScriptException();
return env.Null();
}
if (!args[0].IsBuffer()) {
Napi::TypeError::New(env, "Provided input needs to be a buffer").ThrowAsJavaScriptException();
return env.Null();
}
Buffer<char> buf = args[0].As<Buffer<char>>();
char* pcmData = buf.Data();
opus_int16* pcm = reinterpret_cast<opus_int16*>(pcmData);
int frameSize = buf.Length() / 2 / this->channels;
int compressedLength = opus_encode(this->encoder, pcm, frameSize, &(this->outOpus[0]), MAX_PACKET_SIZE);
Buffer<char> actualBuf = Buffer<char>::Copy(env, reinterpret_cast<char*>(this->outOpus), compressedLength);
if (!actualBuf.IsEmpty()) return actualBuf;
Napi::Error::New(env, "Could not encode the data").ThrowAsJavaScriptException();
return env.Null();
}
Napi::Value NodeOpusEncoder::Decode(const CallbackInfo& args) {
Napi::Env env = args.Env();
if (args.Length() < 1) {
Napi::RangeError::New(env, "Expected 1 argument").ThrowAsJavaScriptException();
return env.Null();
}
if (!args[0].IsBuffer()) {
Napi::TypeError::New(env, "Provided input needs to be a buffer").ThrowAsJavaScriptException();
return env.Null();
}
Buffer<unsigned char> buf = args[0].As<Buffer<unsigned char>>();
unsigned char* compressedData = buf.Data();
size_t compressedDataLength = buf.Length();
if (this->EnsureDecoder() != OPUS_OK) {
Napi::Error::New(env, "Could not create decoder. Check the decoder parameters").ThrowAsJavaScriptException();
return env.Null();
}
int decodedSamples = opus_decode(
this->decoder,
compressedData,
compressedDataLength,
&(this->outPcm[0]),
MAX_FRAME_SIZE,
/* decode_fec */ 0
);
if (decodedSamples < 0) {
Napi::TypeError::New(env, getDecodeError(decodedSamples)).ThrowAsJavaScriptException();
return env.Null();
}
int decodedLength = decodedSamples * 2 * this->channels;
Buffer<char> actualBuf = Buffer<char>::Copy(env, reinterpret_cast<char*>(this->outPcm), decodedLength);
if (!actualBuf.IsEmpty()) return actualBuf;
Napi::Error::New(env, "Could not decode the data").ThrowAsJavaScriptException();
return env.Null();
}
Napi::Value NodeOpusEncoder::Conceal(const CallbackInfo& args) {
Napi::Env env = args.Env();
if (args.Length() < 1) {
Napi::RangeError::New(env, "Expected at least 1 argument").ThrowAsJavaScriptException();
return env.Null();
}
if (!args[0].IsNumber()) {
Napi::TypeError::New(env, "frame_size parameter must be a number").ThrowAsJavaScriptException();
return env.Null();
}
int frame_size = args[0].ToNumber().Int32Value();
// Validate frame_size
if (frame_size <= 0 || frame_size > MAX_FRAME_SIZE) {
Napi::RangeError::New(env, "frame_size must be between 1 and " + std::to_string(MAX_FRAME_SIZE)).ThrowAsJavaScriptException();
return env.Null();
}
// Optional packet parameter for FEC
unsigned char* compressedData = nullptr;
size_t compressedDataLength = 0;
int decode_fec = 0;
if (args.Length() >= 2 && !args[1].IsNull() && !args[1].IsUndefined()) {
if (!args[1].IsBuffer()) {
Napi::TypeError::New(env, "Packet parameter must be a buffer, null, or undefined").ThrowAsJavaScriptException();
return env.Null();
}
Buffer<unsigned char> buf = args[1].As<Buffer<unsigned char>>();
compressedData = buf.Data();
compressedDataLength = buf.Length();
decode_fec = 1; // Enable FEC when packet is provided
}
if (this->EnsureDecoder() != OPUS_OK) {
Napi::Error::New(env, "Could not create decoder. Check the decoder parameters").ThrowAsJavaScriptException();
return env.Null();
}
int decodedSamples = opus_decode(
this->decoder,
compressedData,
compressedDataLength,
&(this->outPcm[0]),
frame_size,
decode_fec
);
if (decodedSamples < 0) {
Napi::TypeError::New(env, getDecodeError(decodedSamples)).ThrowAsJavaScriptException();
return env.Null();
}
int decodedLength = decodedSamples * 2 * this->channels;
Buffer<char> actualBuf = Buffer<char>::Copy(env, reinterpret_cast<char*>(this->outPcm), decodedLength);
if (!actualBuf.IsEmpty()) return actualBuf;
Napi::Error::New(env, "Could not decode the data").ThrowAsJavaScriptException();
return env.Null();
}
void NodeOpusEncoder::ApplyEncoderCTL(const CallbackInfo& args) {
Napi::Env env = args.Env();
if (args.Length() < 2) {
Napi::RangeError::New(env, "Expected 2 arguments").ThrowAsJavaScriptException();
return;
}
if (!args[0].IsNumber() || !args[1].IsNumber()) {
Napi::TypeError::New(env, "Expected ctl and value to be numbers").ThrowAsJavaScriptException();
return;
}
int ctl = args[0].ToNumber().Int32Value();
int value = args[1].ToNumber().Int32Value();
if (this->EnsureEncoder() != OPUS_OK) {
Napi::Error::New(env, "Could not create encoder. Check the encoder parameters").ThrowAsJavaScriptException();
return;
}
if (opus_encoder_ctl(this->encoder, ctl, value) != OPUS_OK) {
Napi::TypeError::New(env, "Invalid ctl / value").ThrowAsJavaScriptException();
return;
}
}
void NodeOpusEncoder::ApplyDecoderCTL(const CallbackInfo& args) {
Napi::Env env = args.Env();
if (args.Length() < 2) {
Napi::RangeError::New(env, "Expected 2 arguments").ThrowAsJavaScriptException();
return;
}
if (!args[0].IsNumber() || !args[1].IsNumber()) {
Napi::TypeError::New(env, "Expected ctl and value to be numbers").ThrowAsJavaScriptException();
return;
}
int ctl = args[0].ToNumber().Int32Value();
int value = args[1].ToNumber().Int32Value();
if (this->EnsureDecoder() != OPUS_OK) {
Napi::Error::New(env, "Could not create decoder. Check the decoder parameters").ThrowAsJavaScriptException();
return;
}
if (opus_decoder_ctl(this->decoder, ctl, value) != OPUS_OK) {
Napi::TypeError::New(env, "Invalid ctl / value").ThrowAsJavaScriptException();
return;
}
}
void NodeOpusEncoder::SetBitrate(const CallbackInfo& args) {
Napi::Env env = args.Env();
if (args.Length() < 1) {
Napi::RangeError::New(env, "Expected 1 argument").ThrowAsJavaScriptException();
return;
}
if (!args[0].IsNumber()) {
Napi::TypeError::New(env, "Expected bitrate to be a number").ThrowAsJavaScriptException();
return;
}
int bitrate = args[0].ToNumber().Int32Value();
if (this->EnsureEncoder() != OPUS_OK) {
Napi::Error::New(env, "Could not create encoder. Check the encoder parameters").ThrowAsJavaScriptException();
return;
}
if (opus_encoder_ctl(this->encoder, OPUS_SET_BITRATE(bitrate)) != OPUS_OK) {
Napi::TypeError::New(env, "Invalid bitrate").ThrowAsJavaScriptException();
return;
}
}
Napi::Value NodeOpusEncoder::GetBitrate(const CallbackInfo& args) {
Napi::Env env = args.Env();
if (this->EnsureEncoder() != OPUS_OK) {
Napi::Error::New(env, "Could not create encoder. Check the encoder parameters").ThrowAsJavaScriptException();
return env.Null();
}
opus_int32 bitrate;
opus_encoder_ctl(this->encoder, OPUS_GET_BITRATE(&bitrate));
return Napi::Number::New(env, bitrate);
}
Object Init(Napi::Env env, Object exports) {
return NodeOpusEncoder::Init(env, exports);
}
NODE_API_MODULE(opus, Init)