Skip to content

Commit

Permalink
Added support for external input sources
Browse files Browse the repository at this point in the history
  • Loading branch information
kiriapurv committed Jun 15, 2015
1 parent d523c2e commit da34bcd
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 17 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ npm start -- [-open] [-wsport websocketPort] [-webport webserverport] [-res widt
|-open | Open streaming url on startup |
|-wsport | Web socket port for streaming media |
|-webport | Web server port |
|-res | Resolution for preview image |
|-res | Resolution for preview image |
|-input | Input source. ( eg. ip camera url) |
9 changes: 7 additions & 2 deletions src/main.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
var WebSocketServer = require("ws").Server;
var cam = require("../build/Release/camera.node");
var cam = require("../build/Debug/camera.node");
var fs = require("fs");
var websocketPort = 9090,
webPort = 9999,
openBrowser = false,
width = 640,
inputString = "",
height = 360;

//Gathering Arguments
Expand All @@ -24,6 +25,9 @@ process.argv.forEach(function (val, index, array) {
width = parseInt(res[0]);
height = parseInt(res[1]);
break;
case "-input":
inputString = array[index + 1];
console.log(inputString);
}
});

Expand Down Expand Up @@ -61,7 +65,8 @@ var connectClient = function (ws) {
width: width,
height: height,
window: false,
codec: ".jpg"
codec: ".jpg",
input: inputString
});
}
clients[index] = ws;
Expand Down
22 changes: 8 additions & 14 deletions src/native/camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
using namespace v8;

//Define functions in scope
std::string* stringValue(Local<Value> value);
std::string stringValue(Local<Value> value);

int m_brk;
int32_t preview_width, preview_height;
Expand Down Expand Up @@ -167,7 +167,7 @@ void Open(const FunctionCallbackInfo<Value>& args) {
//Default Arguments
message->codec = std::string(".jpg");
Local<Value> input = Number::New(isolate,0);
std::string* inputString;
std::string inputString;

//Check if size is passed
if(args.Length() == 2) {
Expand All @@ -186,7 +186,7 @@ void Open(const FunctionCallbackInfo<Value>& args) {
}
if(params->Has(String::NewFromUtf8(isolate,"codec"))) {
Local<String> val = params->Get(String::NewFromUtf8(isolate,"codec"))->ToString();
message->codec = *stringValue(val);
message->codec = stringValue(val);
}
if(params->Has(String::NewFromUtf8(isolate,"input"))) {
input = params->Get(String::NewFromUtf8(isolate,"input"));
Expand All @@ -205,8 +205,8 @@ void Open(const FunctionCallbackInfo<Value>& args) {
message->capture = new cv::VideoCapture();
if(input->IsNumber()) {
message->capture->open((int)input->Int32Value());
} else {
message->capture->open(*inputString);
} else if(!inputString.empty()) {
message->capture->open(inputString);
}
cv::waitKey(10);

Expand All @@ -218,12 +218,6 @@ void Open(const FunctionCallbackInfo<Value>& args) {
uv_async_init(loop,&async,(uv_async_cb)updateAsync);
uv_queue_work(loop, req, CameraOpen,(uv_after_work_cb) CameraClose);

//Free resources
//free #1
if(!input->IsNumber()) {
delete inputString;
}

args.GetReturnValue().Set(String::NewFromUtf8(isolate,"ok"));
}

Expand All @@ -248,16 +242,16 @@ void init(Handle<Object> exports) {
exports->Set(String::NewFromUtf8(isolate,"GetPreviewSize"), FunctionTemplate::New(isolate,GetPreviewSize)->GetFunction());
}

std::string* stringValue(Local<Value> value) {
std::string stringValue(Local<Value> value) {
if(value->IsString()){
//Alloc #1
char * buffer = (char*) malloc(sizeof(char) * value->ToString()->Utf8Length());
value->ToString()->WriteUtf8(buffer,value->ToString()->Utf8Length());
std::string *ret = new std::string(buffer);
std::string ret(buffer);
free(buffer);
return ret;
} else {
return new std::string("");
return "";
}
}

Expand Down

0 comments on commit da34bcd

Please sign in to comment.