-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathScreenRecorderWrapper.mm
125 lines (96 loc) · 4.37 KB
/
ScreenRecorderWrapper.mm
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
#include "ScreenRecorderWrapper.h"
#import "ScreenRecorder.h"
struct Impl {
ScreenRecorder *recorder;
};
Nan::Persistent<v8::Function> ScreenRecorderWrapper::constructor;
ScreenRecorderWrapper::ScreenRecorderWrapper(): pImpl_(new Impl) {}
ScreenRecorderWrapper::~ScreenRecorderWrapper() {}
void ScreenRecorderWrapper::Init(v8::Local<v8::Object> exports) {
Nan::HandleScope scope;
// Prepare constructor template
v8::Local<v8::FunctionTemplate> tpl = Nan::New<v8::FunctionTemplate>(New);
tpl->SetClassName(Nan::New("ScreenRecorder").ToLocalChecked());
tpl->InstanceTemplate()->SetInternalFieldCount(1);
// Prototype
Nan::SetPrototypeMethod(tpl, "start", Start);
Nan::SetPrototypeMethod(tpl, "stop", Stop);
Nan::SetPrototypeMethod(tpl, "setCapturesMouseClicks", SetCapturesMouseClicks);
Nan::SetPrototypeMethod(tpl, "setCapturesCursor", SetCapturesCursor);
Nan::SetPrototypeMethod(tpl, "setCropRect", SetCropRect);
Nan::SetPrototypeMethod(tpl, "setFrameRate", SetFrameRate);
Nan::SetPrototypeMethod(tpl, "recordAudio", RecordAudio);
constructor.Reset(tpl->GetFunction());
exports->Set(Nan::New("ScreenRecorder").ToLocalChecked(), tpl->GetFunction());
}
void ScreenRecorderWrapper::New(const Nan::FunctionCallbackInfo<v8::Value>& info) {
if (info.IsConstructCall()) {
int len = info.Length();
if (len < 1) {
Nan::ThrowTypeError("Wrong number of arguments");
return;
}
if (!info[0]->IsString()) {
Nan::ThrowTypeError("Wrong arguments");
return;
}
CGDirectDisplayID displayId;
if (len > 1 && info[1]->IsNumber()) {
displayId = info[1]->NumberValue();
} else {
displayId = CGMainDisplayID();
}
v8::String::Utf8Value utf8(info[0]->ToString());
NSString *outputPath = [NSString stringWithUTF8String:*utf8];
ScreenRecorder *recorder = [[ScreenRecorder alloc] initWithPath:outputPath andDisplayId:displayId];
ScreenRecorderWrapper *obj = new ScreenRecorderWrapper();
((Impl*)obj->pImpl_)->recorder = recorder;
obj->Wrap(info.This());
info.GetReturnValue().Set(info.This());
} else {
Nan::ThrowTypeError("You must call new ScreenRecorder");
}
}
void ScreenRecorderWrapper::Start(const Nan::FunctionCallbackInfo<v8::Value>& info) {
ScreenRecorderWrapper *obj = ObjectWrap::Unwrap<ScreenRecorderWrapper>(info.Holder());
[((Impl*)obj->pImpl_)->recorder start];
}
void ScreenRecorderWrapper::Stop(const Nan::FunctionCallbackInfo<v8::Value>& info) {
ScreenRecorderWrapper *obj = ObjectWrap::Unwrap<ScreenRecorderWrapper>(info.Holder());
[((Impl*)obj->pImpl_)->recorder stop];
}
void ScreenRecorderWrapper::SetCapturesMouseClicks(const Nan::FunctionCallbackInfo<v8::Value>& info) {
bool capturesMouseClicks = info[0]->IsUndefined() ? false : info[0]->BooleanValue();
ScreenRecorderWrapper *obj = ObjectWrap::Unwrap<ScreenRecorderWrapper>(info.Holder());
[((Impl*)obj->pImpl_)->recorder setCapturesMouseClicks:capturesMouseClicks];
}
void ScreenRecorderWrapper::SetCapturesCursor(const Nan::FunctionCallbackInfo<v8::Value>& info) {
bool capturesCursor = info[0]->IsUndefined() ? false : info[0]->BooleanValue();
ScreenRecorderWrapper *obj = ObjectWrap::Unwrap<ScreenRecorderWrapper>(info.Holder());
[((Impl*)obj->pImpl_)->recorder setCapturesCursor:capturesCursor];
}
void ScreenRecorderWrapper::SetCropRect(const Nan::FunctionCallbackInfo<v8::Value>& info) {
if (info.Length() < 4) {
Nan::ThrowTypeError("Wrong number of arguments");
return;
}
double x = info[0]->NumberValue();
double y = info[1]->NumberValue();
double width = info[2]->NumberValue();
double height = info[3]->NumberValue();
ScreenRecorderWrapper *obj = ObjectWrap::Unwrap<ScreenRecorderWrapper>(info.Holder());
[((Impl*)obj->pImpl_)->recorder setCropRect:CGRectMake(x, y, width, height)];
}
void ScreenRecorderWrapper::SetFrameRate(const Nan::FunctionCallbackInfo<v8::Value>& info) {
if (info.Length() < 1) {
Nan::ThrowTypeError("Wrong number of arguments");
return;
}
int frameRate = info[0]->NumberValue();
ScreenRecorderWrapper *obj = ObjectWrap::Unwrap<ScreenRecorderWrapper>(info.Holder());
[((Impl*)obj->pImpl_)->recorder setFrameRate:frameRate];
}
void ScreenRecorderWrapper::RecordAudio(const Nan::FunctionCallbackInfo<v8::Value>& info) {
ScreenRecorderWrapper *obj = ObjectWrap::Unwrap<ScreenRecorderWrapper>(info.Holder());
[((Impl*)obj->pImpl_)->recorder recordAudio];
}