Skip to content

Commit

Permalink
refactored node-video
Browse files Browse the repository at this point in the history
  • Loading branch information
pkrumins committed Aug 1, 2010
1 parent c92a1eb commit ed8f2cb
Show file tree
Hide file tree
Showing 11 changed files with 1,088 additions and 941 deletions.
17 changes: 17 additions & 0 deletions src/common.cpp
@@ -0,0 +1,17 @@
#include <cstdlib>
#include <cassert>
#include "common.h"

using namespace v8;

Handle<Value>
VException(const char *msg) {
HandleScope scope;
return ThrowException(Exception::Error(String::New(msg)));
}

bool str_eq(const char *s1, const char *s2)
{
return strcmp(s1, s2) == 0;
}

13 changes: 13 additions & 0 deletions src/common.h
@@ -0,0 +1,13 @@
#ifndef COMMON_H
#define COMMON_H

#include <node.h>
#include <cstring>

v8::Handle<v8::Value> VException(const char *msg);
bool str_eq(const char *s1, const char *s2);

typedef enum { BUF_RGB, BUF_BGR, BUF_RGBA, BUF_BGRA } buffer_type;

#endif

205 changes: 205 additions & 0 deletions src/fixed_video.cpp
@@ -0,0 +1,205 @@
#include <node_buffer.h>
#include "common.h"
#include "fixed_video.h"

using namespace v8;
using namespace node;

FixedVideo::FixedVideo(int width, int height) :
videoEncoder(width, height) {}

void
FixedVideo::Initialize(Handle<Object> target)
{
HandleScope scope;

Local<FunctionTemplate> t = FunctionTemplate::New(New);
t->InstanceTemplate()->SetInternalFieldCount(1);
NODE_SET_PROTOTYPE_METHOD(t, "newFrame", NewFrame);
NODE_SET_PROTOTYPE_METHOD(t, "setOutputFile", SetOutputFile);
NODE_SET_PROTOTYPE_METHOD(t, "setQuality", SetQuality);
NODE_SET_PROTOTYPE_METHOD(t, "setFrameRate", SetFrameRate);
NODE_SET_PROTOTYPE_METHOD(t, "setKeyFrameInterval", SetKeyFrameInterval);
NODE_SET_PROTOTYPE_METHOD(t, "end", End);
target->Set(String::NewSymbol("FixedVideo"), t->GetFunction());
}

void
FixedVideo::NewFrame(const unsigned char *data)
{
videoEncoder.newFrame(data);
}

void
FixedVideo::SetOutputFile(const char *fileName)
{
videoEncoder.setOutputFile(fileName);
}

void
FixedVideo::SetQuality(int quality)
{
videoEncoder.setQuality(quality);
}

void
FixedVideo::SetFrameRate(int frameRate)
{
videoEncoder.setFrameRate(frameRate);
}

void
FixedVideo::SetKeyFrameInterval(int keyFrameInterval)
{
videoEncoder.setKeyFrameInterval(keyFrameInterval);
}

void
FixedVideo::End()
{
videoEncoder.end();
}

Handle<Value>
FixedVideo::New(const Arguments &args)
{
HandleScope scope;

if (args.Length() != 2)
return VException("Two arguments required - width and height.");
if (!args[0]->IsInt32())
return VException("First argument must be integer width.");
if (!args[1]->IsInt32())
return VException("Second argument must be integer height.");

int w = args[0]->Int32Value();
int h = args[1]->Int32Value();

if (w < 0)
return VException("Width smaller than 0.");
if (h < 0)
return VException("Height smaller than 0.");

FixedVideo *fv = new FixedVideo(w, h);
fv->Wrap(args.This());
return args.This();
}

Handle<Value>
FixedVideo::NewFrame(const Arguments &args)
{
HandleScope scope;

if (args.Length() != 1)
return VException("One argument required - Buffer with full frame data.");

if (!Buffer::HasInstance(args[0]))
return VException("First argument must be Buffer.");

Buffer *rgb = ObjectWrap::Unwrap<Buffer>(args[0]->ToObject());

FixedVideo *fv = ObjectWrap::Unwrap<FixedVideo>(args.This());
fv->NewFrame((unsigned char *)rgb->data());

return Undefined();
}

Handle<Value>
FixedVideo::SetOutputFile(const Arguments &args)
{
HandleScope scope;

if (args.Length() != 1)
return VException("One argument required - output file name.");

if (!args[0]->IsString())
return VException("First argument must be string.");

String::AsciiValue fileName(args[0]->ToString());

FixedVideo *fv = ObjectWrap::Unwrap<FixedVideo>(args.This());
fv->SetOutputFile(*fileName);

return Undefined();
}

Handle<Value>
FixedVideo::SetQuality(const Arguments &args)
{
HandleScope scope;

if (args.Length() != 1)
return VException("One argument required - video quality.");

if (!args[0]->IsInt32())
return VException("Quality must be integer.");

int q = args[0]->Int32Value();

if (q < 0) return VException("Quality smaller than 0.");
if (q > 63) return VException("Quality greater than 63.");

FixedVideo *fv = ObjectWrap::Unwrap<FixedVideo>(args.This());
fv->SetQuality(q);

return Undefined();
}

Handle<Value>
FixedVideo::SetFrameRate(const Arguments &args)
{
HandleScope scope;

if (args.Length() != 1)
return VException("One argument required - frame rate.");

if (!args[0]->IsInt32())
return VException("Frame rate must be integer.");

int rate = args[0]->Int32Value();

if (rate < 0)
return VException("Frame rate must be positive.");

FixedVideo *fv = ObjectWrap::Unwrap<FixedVideo>(args.This());
fv->SetFrameRate(rate);

return Undefined();
}

Handle<Value>
FixedVideo::SetKeyFrameInterval(const Arguments &args)
{
HandleScope scope;

if (args.Length() != 1)
return VException("One argument required - keyframe interval.");

if (!args[0]->IsInt32())
return VException("Keyframe interval must be integer.");

int interval = args[0]->Int32Value();

if (interval < 0)
return VException("Keyframe interval must be positive.");

if ((interval & (interval - 1)) != 0)
return VException("Keyframe interval must be a power of two.");

FixedVideo *fv = ObjectWrap::Unwrap<FixedVideo>(args.This());
fv->SetKeyFrameInterval(interval);

return Undefined();
}

Handle<Value>
FixedVideo::End(const Arguments &args)
{
HandleScope scope;

FixedVideo *fv = ObjectWrap::Unwrap<FixedVideo>(args.This());
fv->End();

return Undefined();
}

32 changes: 32 additions & 0 deletions src/fixed_video.h
@@ -0,0 +1,32 @@
#ifndef FIXED_VIDEO_H
#define FIXED_VIDEO_H

#include <node.h>

#include "video_encoder.h"

class FixedVideo : public node::ObjectWrap {
VideoEncoder videoEncoder;

public:
FixedVideo(int width, int height);
static void Initialize(v8::Handle<v8::Object> target);
void NewFrame(const unsigned char *data);
void SetOutputFile(const char *fileName);
void SetQuality(int quality);
void SetFrameRate(int frameRate);
void SetKeyFrameInterval(int keyFrameInterval);
void End();

protected:
static v8::Handle<v8::Value> New(const v8::Arguments &args);
static v8::Handle<v8::Value> NewFrame(const v8::Arguments &args);
static v8::Handle<v8::Value> SetOutputFile(const v8::Arguments &args);
static v8::Handle<v8::Value> SetQuality(const v8::Arguments &args);
static v8::Handle<v8::Value> SetFrameRate(const v8::Arguments &args);
static v8::Handle<v8::Value> SetKeyFrameInterval(const v8::Arguments &args);
static v8::Handle<v8::Value> End(const v8::Arguments &args);
};

#endif

14 changes: 14 additions & 0 deletions src/module.cpp
@@ -0,0 +1,14 @@
#include <node.h>

#include "fixed_video.h"
#include "stacked_video.h"

extern "C" void
init(v8::Handle<v8::Object> target)
{
v8::HandleScope scope;

FixedVideo::Initialize(target);
StackedVideo::Initialize(target);
}

0 comments on commit ed8f2cb

Please sign in to comment.