-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathnode_mpg123.cc
More file actions
542 lines (455 loc) · 17.7 KB
/
node_mpg123.cc
File metadata and controls
542 lines (455 loc) · 17.7 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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
/*
* Copyright (c) 2011, Nathan Rajlich <nathan@tootallnate.net>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <v8.h>
#include <node.h>
#include <node_buffer.h>
#include <string.h>
#include "node_pointer.h"
#include "node_mpg123.h"
using namespace v8;
using namespace node;
namespace nodelame {
#define UNWRAP_MH \
HandleScope scope; \
mpg123_handle *mh = reinterpret_cast<mpg123_handle *>(UnwrapPointer(args[0]));
/* not a macro because we're passing function calls in here */
inline int min (int a, int b) {
return a < b ? a : b;
}
Handle<Value> node_mpg123_init (const Arguments& args) {
HandleScope scope;
return scope.Close(Integer::New(mpg123_init()));
}
Handle<Value> node_mpg123_exit (const Arguments& args) {
HandleScope scope;
mpg123_exit();
return Undefined();
}
Handle<Value> node_mpg123_new (const Arguments& args) {
HandleScope scope;
// TODO: Accept an input decoder String
int error = MPG123_OK;
mpg123_handle *mh = mpg123_new(NULL, &error);
Local<Value> rtn;
if (error == MPG123_OK) {
rtn = Local<Value>::New(WrapPointer(mh));
} else {
rtn = Local<Value>::New(Integer::New(error));
}
return scope.Close(rtn);
}
Handle<Value> node_mpg123_current_decoder (const Arguments& args) {
UNWRAP_MH;
const char *decoder = mpg123_current_decoder(mh);
return scope.Close(String::New(decoder));
}
Handle<Value> node_mpg123_supported_decoders (const Arguments& args) {
HandleScope scope;
const char **decoders = mpg123_supported_decoders();
int i = 0;
Handle<Array> rtn = Array::New();
while (*decoders != NULL) {
rtn->Set(i++, String::New(*decoders));
decoders++;
}
return scope.Close(rtn);
}
Handle<Value> node_mpg123_decoders (const Arguments& args) {
HandleScope scope;
const char **decoders = mpg123_decoders();
int i = 0;
Handle<Array> rtn = Array::New();
while (*decoders != NULL) {
rtn->Set(i++, String::New(*decoders));
decoders++;
}
return scope.Close(rtn);
}
Handle<Value> node_mpg123_open_feed (const Arguments& args) {
UNWRAP_MH;
int ret = mpg123_open_feed(mh);
return scope.Close(Integer::New(ret));
}
Handle<Value> node_mpg123_getformat (const Arguments& args) {
UNWRAP_MH;
long rate;
int channels;
int encoding;
int ret;
Local<Value> rtn;
ret = mpg123_getformat(mh, &rate, &channels, &encoding);
if (ret == MPG123_OK) {
Local<Object> o = Object::New();
o->Set(String::NewSymbol("raw_encoding"), Number::New(encoding));
o->Set(String::NewSymbol("sampleRate"), Number::New(rate));
o->Set(String::NewSymbol("channels"), Number::New(channels));
o->Set(String::NewSymbol("signed"), Boolean::New(encoding & MPG123_ENC_SIGNED));
o->Set(String::NewSymbol("float"), Boolean::New(encoding & MPG123_ENC_FLOAT));
o->Set(String::NewSymbol("ulaw"), Boolean::New(encoding & MPG123_ENC_ULAW_8));
o->Set(String::NewSymbol("alaw"), Boolean::New(encoding & MPG123_ENC_ALAW_8));
if (encoding & MPG123_ENC_8)
o->Set(String::NewSymbol("bitDepth"), Integer::New(8));
else if (encoding & MPG123_ENC_16)
o->Set(String::NewSymbol("bitDepth"), Integer::New(16));
else if (encoding & MPG123_ENC_24)
o->Set(String::NewSymbol("bitDepth"), Integer::New(24));
else if (encoding & MPG123_ENC_32 || encoding & MPG123_ENC_FLOAT_32)
o->Set(String::NewSymbol("bitDepth"), Integer::New(32));
else if (encoding & MPG123_ENC_FLOAT_64)
o->Set(String::NewSymbol("bitDepth"), Integer::New(64));
rtn = o;
} else {
rtn = Integer::New(ret);
}
return scope.Close(rtn);
}
Handle<Value> node_mpg123_info (const Arguments& args) {
UNWRAP_MH;
mpg123_frameinfo info;
int ret;
Local<Value> rtn;
ret = mpg123_info(mh, &info);
if (ret == MPG123_OK) {
Local<Object> o = Object::New();
o->Set(String::NewSymbol("version"), Number::New(info.version));
o->Set(String::NewSymbol("layer"), Number::New(info.layer));
o->Set(String::NewSymbol("bitRate"), Number::New(info.bitrate));
o->Set(String::NewSymbol("sampleRate"), Number::New(info.rate));
o->Set(String::NewSymbol("mode"), Number::New(info.mode));
o->Set(String::NewSymbol("modeExtension"), Number::New(info.mode_ext));
o->Set(String::NewSymbol("frameSize"), Number::New(info.framesize));
o->Set(String::NewSymbol("emphasis"), Number::New(info.emphasis));
o->Set(String::NewSymbol("vbr"), Number::New(info.vbr));
o->Set(String::NewSymbol("averageBitRate"), Number::New(info.abr_rate));
rtn = o;
} else {
rtn = Integer::New(ret);
}
return scope.Close(rtn);
}
Handle<Value> node_mpg123_safe_buffer (const Arguments& args) {
HandleScope scope;
return scope.Close(Number::New(mpg123_safe_buffer()));
}
Handle<Value> node_mpg123_outblock (const Arguments& args) {
UNWRAP_MH;
return scope.Close(Number::New(mpg123_outblock(mh)));
}
Handle<Value> node_mpg123_framepos (const Arguments& args) {
UNWRAP_MH;
return scope.Close(Number::New(mpg123_framepos(mh)));
}
Handle<Value> node_mpg123_tell (const Arguments& args) {
UNWRAP_MH;
return scope.Close(Number::New(mpg123_tell(mh)));
}
Handle<Value> node_mpg123_tellframe (const Arguments& args) {
UNWRAP_MH;
return scope.Close(Number::New(mpg123_tellframe(mh)));
}
Handle<Value> node_mpg123_tell_stream (const Arguments& args) {
UNWRAP_MH;
return scope.Close(Number::New(mpg123_tell_stream(mh)));
}
Handle<Value> node_mpg123_feed (const Arguments& args) {
UNWRAP_MH;
// input buffer
char *input = UnwrapPointer(args[1]);
size_t size = args[2]->Int32Value();
feed_req *request = new feed_req;
request->mh = mh;
request->in = (const unsigned char *)input;
request->size = size;
request->callback = Persistent<Function>::New(Local<Function>::Cast(args[3]));
request->req.data = request;
uv_queue_work(uv_default_loop(), &request->req,
node_mpg123_feed_async,
(uv_after_work_cb)node_mpg123_feed_after);
return Undefined();
}
void node_mpg123_feed_async (uv_work_t *req) {
feed_req *r = (feed_req *)req->data;
r->rtn = mpg123_feed(
r->mh,
r->in,
r->size
);
}
void node_mpg123_feed_after (uv_work_t *req) {
HandleScope scope;
feed_req *r = (feed_req *)req->data;
Handle<Value> argv[1];
argv[0] = Integer::New(r->rtn);
TryCatch try_catch;
r->callback->Call(Context::GetCurrent()->Global(), 1, argv);
// cleanup
r->callback.Dispose();
delete r;
if (try_catch.HasCaught()) {
FatalException(try_catch);
}
}
Handle<Value> node_mpg123_read (const Arguments& args) {
UNWRAP_MH;
// output buffer
char *output = UnwrapPointer(args[1]);
size_t size = args[2]->Int32Value();
read_req *request = new read_req;
request->mh = mh;
request->out = (unsigned char *)output;
request->size = size;
request->done = 0;
request->callback = Persistent<Function>::New(Local<Function>::Cast(args[3]));
request->req.data = request;
uv_queue_work(uv_default_loop(), &request->req,
node_mpg123_read_async,
(uv_after_work_cb)node_mpg123_read_after);
return Undefined();
}
void node_mpg123_read_async (uv_work_t *req) {
read_req *r = (read_req *)req->data;
r->rtn = mpg123_read(
r->mh,
r->out,
r->size,
&r->done
);
/* any new metadata? */
r->meta = mpg123_meta_check(r->mh);
}
void node_mpg123_read_after (uv_work_t *req) {
HandleScope scope;
read_req *r = (read_req *)req->data;
Handle<Value> argv[3];
argv[0] = Integer::New(r->rtn);
argv[1] = Integer::New(r->done);
argv[2] = Integer::New(r->meta);
TryCatch try_catch;
r->callback->Call(Context::GetCurrent()->Global(), 3, argv);
// cleanup
r->callback.Dispose();
delete r;
if (try_catch.HasCaught()) {
FatalException(try_catch);
}
}
Handle<Value> node_mpg123_id3 (const Arguments& args) {
UNWRAP_MH;
id3_req *request = new id3_req;
request->mh = mh;
request->callback = Persistent<Function>::New(Local<Function>::Cast(args[1]));
request->req.data = request;
uv_queue_work(uv_default_loop(), &request->req,
node_mpg123_id3_async,
(uv_after_work_cb)node_mpg123_id3_after);
return Undefined();
}
void node_mpg123_id3_async (uv_work_t *req) {
id3_req *r = (id3_req *)req->data;
r->rtn = mpg123_id3(
r->mh,
&r->v1,
&r->v2
);
}
void node_mpg123_id3_after (uv_work_t *req) {
HandleScope scope;
id3_req *ireq = (id3_req *)req->data;
mpg123_id3v1 *v1 = ireq->v1;
mpg123_id3v2 *v2 = ireq->v2;
int r = ireq->rtn;
Handle<Value> rtn;
if (r == MPG123_OK) {
if (v1 != NULL) {
/* got id3v1 tags */
Local<Object> o = Object::New();
#define SET(prop) \
o->Set(String::NewSymbol(#prop), String::New(v1->prop, min(sizeof(v1->prop), v1->prop == NULL ? 0 : strlen(v1->prop))));
SET(tag);
SET(title);
SET(artist);
SET(album);
SET(year);
if (v1->comment[28] == 0 && v1->comment[29] >= 1) {
/* ID3v1.1 */
o->Set(String::NewSymbol("comment"), String::New(v1->comment, min(sizeof(v1->comment) - 2, v1->comment == NULL ? 0 : strlen(v1->comment))));
o->Set(String::NewSymbol("trackNumber"), Integer::New(v1->comment[29]));
} else {
/* ID3v1 */
SET(comment);
}
o->Set(String::NewSymbol("genre"), Integer::New(v1->genre));
rtn = o;
#undef SET
} else if (v2 != NULL) {
/* got id3v2 tags */
mpg123_string *s;
mpg123_text *t;
Local<Object> o = Object::New();
Local<Array> a;
Local<Object> text;
#define SET(prop) \
s = v2->prop; \
if (s != NULL) \
o->Set(String::NewSymbol(#prop), String::New(s->p, mpg123_strlen(s, 1)));
SET(title)
SET(artist)
SET(album)
SET(year)
SET(genre)
SET(comment)
#undef SET
#define SET_ARRAY(array, count) \
a = Array::New(v2->count); \
for (size_t i = 0; i < v2->count; i++) { \
t = &v2->array[i]; \
text = Object::New(); \
a->Set(i, text); \
text->Set(String::NewSymbol("lang"), String::New(t->lang, min(sizeof(t->lang), strlen(t->lang)))); \
text->Set(String::NewSymbol("id"), String::New(t->id, min(sizeof(t->id), strlen(t->id)))); \
s = &t->description; \
if (s != NULL) \
text->Set(String::NewSymbol("description"), String::New(s->p, mpg123_strlen(s, 1))); \
s = &t->text; \
if (s != NULL) \
text->Set(String::NewSymbol("text"), String::New(s->p, mpg123_strlen(s, 1))); \
} \
o->Set(String::NewSymbol(#count), a);
SET_ARRAY(comment_list, comments)
SET_ARRAY(text, texts)
SET_ARRAY(extra, extras)
#undef SET_ARRAY
rtn = o;
} else {
rtn = Null();
}
}
Handle<Value> argv[2];
argv[0] = Integer::New(ireq->rtn);
argv[1] = rtn;
TryCatch try_catch;
ireq->callback->Call(Context::GetCurrent()->Global(), 2, argv);
// cleanup
ireq->callback.Dispose();
delete ireq;
if (try_catch.HasCaught()) {
FatalException(try_catch);
}
}
void InitMPG123(Handle<Object> target) {
HandleScope scope;
#define CONST_INT(value) \
target->Set(String::NewSymbol(#value), Integer::New(value), \
static_cast<PropertyAttribute>(ReadOnly|DontDelete));
// mpg123_errors
CONST_INT(MPG123_DONE); /**< Message: Track ended. Stop decoding. */
CONST_INT(MPG123_NEW_FORMAT); /**< Message: Output format will be different on next call. Note that some libmpg123 versions between 1.4.3 and 1.8.0 insist on you calling mpg123_getformat() after getting this message code. Newer verisons behave like advertised: You have the chance to call mpg123_getformat(), but you can also just continue decoding and get your data. */
CONST_INT(MPG123_NEED_MORE); /**< Message: For feed reader: "Feed me more!" (call mpg123_feed() or mpg123_decode() with some new input data). */
CONST_INT(MPG123_ERR); /**< Generic Error */
CONST_INT(MPG123_OK); /**< Success */
CONST_INT(MPG123_BAD_OUTFORMAT); /**< Unable to set up output format! */
CONST_INT(MPG123_BAD_CHANNEL); /**< Invalid channel number specified. */
CONST_INT(MPG123_BAD_RATE); /**< Invalid sample rate specified. */
CONST_INT(MPG123_ERR_16TO8TABLE); /**< Unable to allocate memory for 16 to 8 converter table! */
CONST_INT(MPG123_BAD_PARAM); /**< Bad parameter id! */
CONST_INT(MPG123_BAD_BUFFER); /**< Bad buffer given -- invalid pointer or too small size. */
CONST_INT(MPG123_OUT_OF_MEM); /**< Out of memory -- some malloc() failed. */
CONST_INT(MPG123_NOT_INITIALIZED); /**< You didn't initialize the library! */
CONST_INT(MPG123_BAD_DECODER); /**< Invalid decoder choice. */
CONST_INT(MPG123_BAD_HANDLE); /**< Invalid mpg123 handle. */
CONST_INT(MPG123_NO_BUFFERS); /**< Unable to initialize frame buffers (out of memory?). */
CONST_INT(MPG123_BAD_RVA); /**< Invalid RVA mode. */
CONST_INT(MPG123_NO_GAPLESS); /**< This build doesn't support gapless decoding. */
CONST_INT(MPG123_NO_SPACE); /**< Not enough buffer space. */
CONST_INT(MPG123_BAD_TYPES); /**< Incompatible numeric data types. */
CONST_INT(MPG123_BAD_BAND); /**< Bad equalizer band. */
CONST_INT(MPG123_ERR_NULL); /**< Null pointer given where valid storage address needed. */
CONST_INT(MPG123_ERR_READER); /**< Error reading the stream. */
CONST_INT(MPG123_NO_SEEK_FROM_END);/**< Cannot seek from end (end is not known). */
CONST_INT(MPG123_BAD_WHENCE); /**< Invalid 'whence' for seek function.*/
CONST_INT(MPG123_NO_TIMEOUT); /**< Build does not support stream timeouts. */
CONST_INT(MPG123_BAD_FILE); /**< File access error. */
CONST_INT(MPG123_NO_SEEK); /**< Seek not supported by stream. */
CONST_INT(MPG123_NO_READER); /**< No stream opened. */
CONST_INT(MPG123_BAD_PARS); /**< Bad parameter handle. */
CONST_INT(MPG123_BAD_INDEX_PAR); /**< Bad parameters to mpg123_index() and mpg123_set_index() */
CONST_INT(MPG123_OUT_OF_SYNC); /**< Lost track in bytestream and did not try to resync. */
CONST_INT(MPG123_RESYNC_FAIL); /**< Resync failed to find valid MPEG data. */
CONST_INT(MPG123_NO_8BIT); /**< No 8bit encoding possible. */
CONST_INT(MPG123_BAD_ALIGN); /**< Stack aligmnent error */
CONST_INT(MPG123_NULL_BUFFER); /**< NULL input buffer with non-zero size... */
CONST_INT(MPG123_NO_RELSEEK); /**< Relative seek not possible (screwed up file offset) */
CONST_INT(MPG123_NULL_POINTER); /**< You gave a null pointer somewhere where you shouldn't have. */
CONST_INT(MPG123_BAD_KEY); /**< Bad key value given. */
CONST_INT(MPG123_NO_INDEX); /**< No frame index in this build. */
CONST_INT(MPG123_INDEX_FAIL); /**< Something with frame index went wrong. */
CONST_INT(MPG123_BAD_DECODER_SETUP); /**< Something prevents a proper decoder setup */
CONST_INT(MPG123_MISSING_FEATURE); /**< This feature has not been built into libmpg123. */
CONST_INT(MPG123_BAD_VALUE); /**< A bad value has been given, somewhere. */
CONST_INT(MPG123_LSEEK_FAILED); /**< Low-level seek failed. */
CONST_INT(MPG123_BAD_CUSTOM_IO); /**< Custom I/O not prepared. */
CONST_INT(MPG123_LFS_OVERFLOW); /**< Offset value overflow during translation of large file API calls -- your client program cannot handle that large file. */
/* mpg123_enc_enum */
CONST_INT(MPG123_ENC_8);
CONST_INT(MPG123_ENC_16);
CONST_INT(MPG123_ENC_24);
CONST_INT(MPG123_ENC_32);
CONST_INT(MPG123_ENC_SIGNED);
CONST_INT(MPG123_ENC_FLOAT);
CONST_INT(MPG123_ENC_SIGNED_16);
CONST_INT(MPG123_ENC_UNSIGNED_16);
CONST_INT(MPG123_ENC_UNSIGNED_8);
CONST_INT(MPG123_ENC_SIGNED_8);
CONST_INT(MPG123_ENC_ULAW_8);
CONST_INT(MPG123_ENC_ALAW_8);
CONST_INT(MPG123_ENC_SIGNED_32);
CONST_INT(MPG123_ENC_UNSIGNED_32);
CONST_INT(MPG123_ENC_SIGNED_24);
CONST_INT(MPG123_ENC_UNSIGNED_24);
CONST_INT(MPG123_ENC_FLOAT_32);
CONST_INT(MPG123_ENC_FLOAT_64);
CONST_INT(MPG123_ENC_ANY);
/* mpg123_channelcount */
CONST_INT(MPG123_MONO);
CONST_INT(MPG123_STEREO);
/* mpg123_channels */
CONST_INT(MPG123_LEFT);
CONST_INT(MPG123_RIGHT);
CONST_INT(MPG123_LR);
CONST_INT(MPG123_ID3);
CONST_INT(MPG123_NEW_ID3);
CONST_INT(MPG123_ICY);
CONST_INT(MPG123_NEW_ICY);
NODE_SET_METHOD(target, "mpg123_init", node_mpg123_init);
NODE_SET_METHOD(target, "mpg123_exit", node_mpg123_exit);
NODE_SET_METHOD(target, "mpg123_new", node_mpg123_new);
NODE_SET_METHOD(target, "mpg123_decoders", node_mpg123_decoders);
NODE_SET_METHOD(target, "mpg123_current_decoder", node_mpg123_current_decoder);
NODE_SET_METHOD(target, "mpg123_supported_decoders", node_mpg123_supported_decoders);
NODE_SET_METHOD(target, "mpg123_getformat", node_mpg123_getformat);
NODE_SET_METHOD(target, "mpg123_info", node_mpg123_info);
NODE_SET_METHOD(target, "mpg123_safe_buffer", node_mpg123_safe_buffer);
NODE_SET_METHOD(target, "mpg123_outblock", node_mpg123_outblock);
NODE_SET_METHOD(target, "mpg123_framepos", node_mpg123_framepos);
NODE_SET_METHOD(target, "mpg123_tell", node_mpg123_tell);
NODE_SET_METHOD(target, "mpg123_tellframe", node_mpg123_tellframe);
NODE_SET_METHOD(target, "mpg123_tell_stream", node_mpg123_tell_stream);
NODE_SET_METHOD(target, "mpg123_open_feed", node_mpg123_open_feed);
NODE_SET_METHOD(target, "mpg123_feed", node_mpg123_feed);
NODE_SET_METHOD(target, "mpg123_read", node_mpg123_read);
NODE_SET_METHOD(target, "mpg123_id3", node_mpg123_id3);
}
} // nodelame namespace