Skip to content

Commit

Permalink
fix mpg123 binding build
Browse files Browse the repository at this point in the history
  • Loading branch information
TooTallNate committed Oct 14, 2012
1 parent 4a987cc commit c83ca03
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 27 deletions.
36 changes: 16 additions & 20 deletions src/node_mpg123.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <v8.h>
#include <node.h>
#include <node_buffer.h>
#include "node_pointer.h"

#include "mpg123.h"

Expand All @@ -27,14 +28,11 @@ namespace nodelame {

#define UNWRAP_MH \
HandleScope scope; \
Local<Object>wrapper = args[0]->ToObject(); \
mpg123_handle *mh = (mpg123_handle *)wrapper->GetPointerFromInternalField(0);

/* Wrapper ObjectTemplate to hold `mpg123_handle` instances */
Persistent<ObjectTemplate> mhClass;
mpg123_handle *mh = reinterpret_cast<mpg123_handle *>(UnwrapPointer(args[0]));

/* struct used for async decoding */
struct decode_req {
uv_work_t req;
mpg123_handle *mh;
const unsigned char *input;
size_t insize;
Expand All @@ -56,85 +54,86 @@ Handle<Value> node_mpg123_exit (const Arguments& args) {
return Undefined();
}

/*
void mh_weak_callback (Persistent<Value> wrapper, void *arg) {
HandleScope scope;
mpg123_handle *mh = (mpg123_handle *)arg;
mpg123_delete(mh);
wrapper.Dispose();
}
*/

// TODO: Make async
Handle<Value> node_mpg123_new (const Arguments& args) {
HandleScope scope;

// TODO: Accept an input decoder String
int error = 0;
int error = MPG123_OK;
mpg123_handle *mh = mpg123_new(NULL, &error);

Handle<Value> rtn;
if (error == MPG123_OK) {
Persistent<Object> o = Persistent<Object>::New(mhClass->NewInstance());
o->SetPointerInInternalField(0, mh);
o.MakeWeak(mh, mh_weak_callback);
rtn = o;
rtn = WrapPointer(mh);
} else {
rtn = 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(Integer::New(i++), String::New(*decoders));
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(Integer::New(i++), String::New(*decoders));
rtn->Set(i++, String::New(*decoders));
decoders++;
}
return scope.Close(rtn);
}

// TODO: Make async

Handle<Value> node_mpg123_open_feed (const Arguments& args) {
UNWRAP_MH;
int ret = mpg123_open_feed(mh);
return scope.Close(Integer::New(ret));
}


// TODO: Make async
Handle<Value> node_mpg123_decode (const Arguments& args) {
UNWRAP_MH;

// input MP3 data
const unsigned char *input = NULL;
if (!args[1]->IsNull()) {
input = (const unsigned char *)Buffer::Data(args[1]->ToObject());
input = (const unsigned char *)UnwrapPointer(args[1]);
}
size_t insize = args[2]->Int32Value();

// the output buffer
Local<Object> outbuf = args[3]->ToObject();
int out_offset = args[4]->Int32Value();
unsigned char *output = (unsigned char *)(Buffer::Data(outbuf) + out_offset);
unsigned char *output = (unsigned char *)UnwrapPointer(args[3], out_offset);
size_t outsize = args[5]->Int32Value();

size_t size = 0;
Expand All @@ -161,9 +160,6 @@ Handle<Value> node_mpg123_decode (const Arguments& args) {
void InitMPG123(Handle<Object> target) {
HandleScope scope;

mhClass = Persistent<ObjectTemplate>::New(ObjectTemplate::New());
mhClass->SetInternalFieldCount(1);

#define CONST_INT(value) \
target->Set(String::NewSymbol(#value), Integer::New(value), \
static_cast<PropertyAttribute>(ReadOnly|DontDelete));
Expand Down
14 changes: 7 additions & 7 deletions src/node_pointer.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,33 +18,33 @@ inline static void wrap_pointer_cb(char *data, void *hint) {
* Wraps "ptr" into a new SlowBuffer instance with size "length".
*/

inline static v8::Handle<v8::Value> WrapPointer(char *ptr, size_t length) {
inline static v8::Handle<v8::Value> WrapPointer(void *ptr, size_t length) {
void *user_data = NULL;
node::Buffer *buf = node::Buffer::New(ptr, length, wrap_pointer_cb, user_data);
node::Buffer *buf = node::Buffer::New((char *)ptr, length, wrap_pointer_cb, user_data);
return buf->handle_;
}

/*
* Wraps "ptr" into a new SlowBuffer instance with length 0.
*/

inline static v8::Handle<v8::Value> WrapPointer(char *ptr) {
return WrapPointer(ptr, 0);
inline static v8::Handle<v8::Value> WrapPointer(void *ptr) {
return WrapPointer((char *)ptr, 0);
}

/*
* Unwraps Buffer instance "buffer" to a C `char *` with the offset specified.
*/

inline static char * UnwrapPointer(v8::Handle<v8::Value> buffer, int64_t offset) {
return node::Buffer::Data(buffer.As<Object>()) + offset;
return node::Buffer::Data(buffer.As<v8::Object>()) + offset;
}

/*
* Unwraps Buffer instance "buffer" to a C `char *` (no offset applied).
*/


inline static char * UnwrapPointer(v8::Handle<v8::Value>) {
return node::Buffer::Data(buffer.As<Object>);
inline static char * UnwrapPointer(v8::Handle<v8::Value> buffer) {
return node::Buffer::Data(buffer.As<v8::Object>());
}

0 comments on commit c83ca03

Please sign in to comment.