-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcastro.js
112 lines (94 loc) · 3.6 KB
/
castro.js
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
// Castro: Screen*cast ro*bot
// Copyright (c) 2014-2016 Jason Huggins <jrhuggins@gmail.com>
// License: MIT
// TODO:
// Custom recording rect
// Handle pause/restart
// Capture frame/screenshots: (https://developer.apple.com/library/ios/qa/qa1702/_index.html)
var $ = require('nodobjc');
$.framework('AVFoundation');
$.framework('Foundation');
var Castro = function(){
this._started = false;
this._used = false;
this.pool = $.NSAutoreleasePool('alloc')('init');
this.session = $.AVCaptureSession('alloc')('init');
// Set the main display as capture input
this.displayId = $.CGMainDisplayID();
this.input = $.AVCaptureScreenInput('alloc')('initWithDisplayID', this.displayId);
if (this.session('canAddInput', this.input)) {
this.session('addInput', this.input);
}
// Set a movie file as output
this.movieFileOutput = $.AVCaptureMovieFileOutput('alloc')('init');
if (this.session('canAddOutput', this.movieFileOutput)) {
this.session('addOutput', this.movieFileOutput);
}
this.session('startRunning');
this.setLocation();
}
Castro.prototype = {
// Set recording file location
setLocation: function(path) {
// TODO: Does file exist at the file location path? If so, do something about it...
//var defaultManager = $.NSFileManager('alloc')('init')
//if (defaultManager('fileExistsAtPath',NSlocation)) {
// console.log("File already exists!")
//}
if (!path){
// Default Destination: e.g. "/Users/hugs/Desktop/Castro_uul3di.mov"
var homeDir = $.NSHomeDirectory();
var desktopDir = homeDir.toString() + '/Desktop/';
var randomString = (Math.random() + 1).toString(36).substring(12);
var filename = 'Castro_' + randomString + '.mp4';
this.location = desktopDir + filename;
} else {
// TODO: Make sure path is legit.
this.location = path;
}
this.NSlocation = $.NSString('stringWithUTF8String', this.location);
this.NSlocationURL = $.NSURL('fileURLWithPath', this.NSlocation);
},
// Start recording
start: function() {
if (!this._started) {
if (!this._used) {
this.movieFileOutput('startRecordingToOutputFileURL', this.NSlocationURL,
'recordingDelegate', this.movieFileOutput);
this._started = true;
} else {
throw new Error("Recording has completed. To make a new recording, create a new Castro object.");
}
} else {
throw new Error("A recording is already in progress.");
}
},
// Stop recording
stop: function() {
if (!this._used) {
if (this._started) {
this.movieFileOutput('stopRecording');
this.pool('drain');
this._started = false;
this._used = true;
return this.location;
} else {
throw new Error("Try starting it first!");
}
} else {
throw new Error("Recording has completed. To make a new recording, create a new Castro object.");
}
},
test: function() {
console.log("Castro will record the main display for 10 seconds...");
console.log("Now starting...");
this.start();
setTimeout(function(_this){
console.log("Now stopping...");
_this.stop();
console.log("File location:");
console.log(_this.location);
}, 10*1000, this);
}
}
module.exports.Castro = Castro;