-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathnode-opus.cc
213 lines (164 loc) · 6.07 KB
/
node-opus.cc
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
#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 OpusEncoder::Init(Napi::Env env, Object exports) {
HandleScope scope(env);
Function func = DefineClass(env, "OpusEncoder", {
InstanceMethod("encode", &OpusEncoder::Encode),
InstanceMethod("decode", &OpusEncoder::Decode),
InstanceMethod("applyEncoderCTL", &OpusEncoder::ApplyEncoderCTL),
InstanceMethod("applyDecoderCTL", &OpusEncoder::ApplyDecoderCTL),
InstanceMethod("setBitrate", &OpusEncoder::SetBitrate),
InstanceMethod("getBitrate", &OpusEncoder::GetBitrate),
});
exports.Set("OpusEncoder", func);
return exports;
}
OpusEncoder::OpusEncoder(const CallbackInfo& args): ObjectWrap<OpusEncoder>(args) {
this->encoder = nullptr;
this->decoder = nullptr;
this->rate = args[0].ToNumber().Int32Value();
this->channels = args[1].ToNumber().Int32Value();
this->application = OPUS_APPLICATION_AUDIO;
this->outPcm = new opus_int16[channels * MAX_FRAME_SIZE];
}
OpusEncoder::~OpusEncoder() {
if (this->encoder) opus_encoder_destroy(this->encoder);
if (this->decoder) opus_decoder_destroy(this->decoder);
this->encoder = nullptr;
this->decoder = nullptr;
delete this->outPcm;
this->outPcm = nullptr;
}
int OpusEncoder::EnsureEncoder() {
if (this->encoder) return 0;
int error;
this->encoder = opus_encoder_create(rate, channels, application, &error);
return error;
}
int OpusEncoder::EnsureDecoder() {
if (this->decoder) return 0;
int error;
this->decoder = opus_decoder_create(rate, channels, &error);
return error;
}
Napi::Value OpusEncoder::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[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::Value OpusEncoder::Decode(const CallbackInfo& args) {
Napi::Env env = args.Env();
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;
}
void OpusEncoder::ApplyEncoderCTL(const CallbackInfo& args) {
Napi::Env env = args.Env();
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 OpusEncoder::ApplyDecoderCTL(const CallbackInfo& args) {
Napi::Env env = args.Env();
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 OpusEncoder::SetBitrate(const CallbackInfo& args) {
Napi::Env env = args.Env();
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 OpusEncoder::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 OpusEncoder::Init(env, exports);
}
NODE_API_MODULE(opus, Init)